1 回答

TA贡献1848条经验 获得超6个赞
您需要在回调中获取 idsetState以确保它已被设置:
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
fetch('https://url.com/daily-ecommerce/', {
method: 'GET',
headers: {
'Authorization': `Token xxxxx`
}
})
.then(res => res.json())
.then(jsonRes => this.setState({
task: jsonRes
}, () => {
fetch(`https://url.com/step/?task_id=${this.state.task[0].id}`, {
method: 'GET',
headers: {
'Authorization': `Token xxxxx`
}
})
}))
.catch(error => console.log(error))
}
另一种方法是直接从 json 响应中传递 id:
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
fetch('https://url.com/daily-ecommerce/', {
method: 'GET',
headers: {
'Authorization': `Token xxxxx`
}
})
.then(res => res.json())
.then(jsonRes => {
this.setState({
task: jsonRes
})
fetch(`https://url.com/step/?task_id=${jsonRes[0].id}`, {
method: 'GET',
headers: {
'Authorization': `Token xxxxx`
}
})
})
.catch(error => console.log(error))
}
添加回答
举报