Does this.setState return promise in react

后端 未结 6 1191
陌清茗
陌清茗 2020-12-03 07:19

I made my componentWillMount() async. Now I can using await with the setState.

Here is the sample code:

compon         


        
6条回答
  •  再見小時候
    2020-12-03 08:14

    You can promisify this.setState so that you can use the React API as a promise. This is how I got it to work:

    class LyricsGrid extends Component {
    
      setAsyncState = (newState) =>
        new Promise((resolve) => this.setState(newState, resolve));
    

    Later, I call this.setAsyncState using the standard Promise API:

    this.setAsyncState({ lyricsCorpus, matrix, count })
      .then(foo1)
      .then(foo2)
      .catch(err => console.error(err))
    

提交回复
热议问题