updata(){
let newData;
fetch('http://127.0.0.1:8080/news',{method:'post',headers:{}})
.then((response) => response.json())
.then((responseJson) => {
newData=responseJson.greet;this.setState({refreshing:false});alert(newData)
})
.catch((error) => {
console.error(error);
});
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.setState({
dataSource:ds.cloneWithRows(['ff',newData])
})
}
这边最后一行dataSource:ds.cloneWithRows(['ff',newData]),显示newData是空的,但上面alert显示newData值时字符串‘hello’,这是为啥?
setState
放到回调里面去