1 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊
您需要在回調(diào)中獲取 idsetState以確保它已被設(shè)置:
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 響應(yīng)中傳遞 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))
}
添加回答
舉報(bào)