【发布时间】:2017-12-19 16:28:58
【问题描述】:
我有一个问题,在我使用 fetch 之后,我的 this.state 是未定义的,我不明白为什么......
googlePOI(center) {
this.setState({markerPOI: []});
var i = 0;
let call = '';
call = call.concat(URL_POI, center.lat.toString(), ',', center.lng.toString(), ARG_POI, this.state.filters[i], KEY);
console.log(this.state) // DEFINE !
fetch(call)
.then((answer) => answer.json())
.then(function(answer) {
console.log(this.state) // UNDEFINE !
answer.results.map((item, index) => {
let geo = item.geometry.location;
let marker = <MapView.Marker title={item.name} coordinate={{latitude: geo.lat, longitude: geo.lng}} />
})
}).catch(function(error) {
console.log("ERROR: fetch googlePOI -> "+error);
})
}
【问题讨论】:
-
一方面,您永远不会使用
this.setState({ someState: someValue })从获取中更新您的状态。其次,如果this.state完全未定义,您应该为组件的其余部分提供代码,当然是您的构造函数。
标签: react-native fetch state