简体   繁体   中英

Async React component lifecycle methods

If lifecycle methods on React components are marked async , are they treated differently?

    default class MyComponent extends Component {
      state = {};

      async componentDidMount() {
        await this.fillQuestions();
      }
    }

There's no difference between the way they're being called. async only means that the function will return Promise. and since React doesn't expect any return value, there's no difference between the two from React's point of view.

However, you can use async when you want to await for some other promise inside your lifecycle hook.

Take a look at the following medium article: https://medium.com/front-end-weekly/async-await-with-react-lifecycle-methods-802e7760d802

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM