gpt4 book ai didi

javascript - componentDidMount()中接收到数据后调用函数和get方法

转载 作者:行者123 更新时间:2023-12-02 23:16:11 26 4
gpt4 key购买 nike

预期效果:在 componentDidMount () 中,我下载 s 并将其保存在变量 timeId 中。如果 timeId 为 true,则将 this.state.timeId 传递给 loadTime () 函数到 https://app/load- id/${id} 并调用此函数。该函数返回的数据保存在变量checkId中。 this.state.checkId 传输到Details 组件。问题:在componentDidMount()中接收到数据后,如何调用函数loadId()

应用程序

class App extends Component {
constructor (props) {
super(props);
this.state = {
checkId: '',
timeId: ''
}
}

componentDidMount() {
axios({
url: `https://app`,
method: "GET",
headers: {
'Authorization': `Bearer ${token}`
}
})
.then(res => {
this.setState({
timeId: res.data.id,
});
})
.catch(error => {
console.log(error);
})

}

loadId = (id) => { //id ---> this.state.timeId
axios({
url: `https://app/load-id/${id}`,
method: "GET",
headers: {
'Authorization': `Bearer ${token}`
}
})
.then(response => {
console.log(response);
this.setState({
checkId: response.data
});
})
.catch(error => {
console.log(error);
})
}



render () {

return (
<div>
<Item

/>

<Details
checkId = {this.state.checkId}
/>
</div>
)
}
}

详细信息

class Details extends React.Component {
constructor(props) {
super(props);

this.state = {
task: ''
};
}


componentDidUpdate(previousProps, previousState) {
if (previousProps.checkId !== this.props.checkId) {

this.setState({
task: this.props.checkId
})
}

render() {
return (
<div >

</div>
);
}
}

最佳答案

您需要在 then 函数中调用 loadId。

    axios({
url: `https://app`,
method: "GET",
headers: {
'Authorization': `Bearer ${token}`
}
})
.then(res => {
this.setState({
timeId: res.data.id,
});
this.loadId(res.data.id);
})
.catch(error => {
console.log(error);
})

关于javascript - componentDidMount()中接收到数据后调用函数和get方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57155038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com