【发布时间】:2017-02-27 11:37:25
【问题描述】:
我是 React-Native 的新手
我有fetch,通过它我可以得到一些数据。我想要做的是在请求结束并且数据准备好之后调用另一个函数或更新状态。这是我的代码。
getProducts()
{
return fetch(prodUrl, {method: "GET"})
.then((response) => response.json())
.then((responseData) => {
this.setState({brandList: responseData.products});
console.log("Brands State -> : ",this.state.brandList)
})
.done();
}
我在componentWillMount() 中调用此getProducts() 函数,并尝试在render() 中使用获取的数据。
设置状态后,尝试console.log() 时看不到变化,很可能是因为fetch() 是异步的。如何在fetch() 结束之前停止执行render() 函数?或者你能推荐任何其他请求类型,而不是 fetch() 同步。
【问题讨论】:
标签: javascript asynchronous react-native request fetch