Skip to content Skip to sidebar Skip to footer

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),
    })  
  }

Solution 3:

Or another way is

asynccomponentDidMount() {
    const { data: dataSource = [] } = awaitthis.getData();   
    this.setState({dataSource})  
  }

This will copy your data to a inmutable object an reasign the name, also, set a default value to the object dataSource

Post a Comment for "React-native Async Function Returns Promise But Not My Json Data?"