React-native Async Function Returns Promise But Not My Json Data?
I'm learning react-native, and I'm running into an issue. Why does getting data on return from an async function return a promise, but in the async function itself, it correctly re
Solution 1:
Since getData()
is a promise, you should be able to obtain the data in a then
block as follows:
componentDidMount() {
this.getData()
.then((data) => {
this.setState({
dataSource:this.state.dataSource.cloneWithRows(data),
})
});
}
Solution 2:
Another approach similar to the original code of the questioner:
asynccomponentDidMount() {
let data = awaitthis.getData();
console.log(data);
this.setState({
dataSource:this.state.dataSource.cloneWithRows(data),
})
}
Post a Comment for "React-native Async Function Returns Promise But Not My Json Data?"