【发布时间】:2018-12-31 06:13:36
【问题描述】:
我正在制作一个从 API 接收数据的应用。获得这些数据后,我想使用从第一次调用中获得的端点再次调用同一个 API。
fetch(req)
.then((response)=>(
response.json()
)).then((json)=>{
console.log(json)
json.meals.map((obj)=>{
let url = `https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/${obj.id}/information`
let req = new Request(url,{
method: 'GET',
headers: header
})
fetch(req)
.then((response)=>(
response.json()
)).then((json)=>{
console.log(json);
this.setState((prevState)=>{
recipe: prevState.recipe.push(json)
})
})
})
this.setState(()=>{
return{
data: json
}
})
})
我在这里发出了两个获取请求,但问题是第一个响应中的数据是在第二个获取请求之后输出的。此外,state: data 设置在 state: recipe 之前,组件使用来自 state: data 的数据进行渲染。
render(){
return(
<div className="my-container">
<EnterCalorie getData={this.getData}/>
<MealData data={this.state.data} recipe={this.state.recipe}/>
</div>
)
}
如何确保两者同时传下来?
【问题讨论】:
-
兑现承诺并再次致电
标签: javascript html reactjs fetch-api