Ben Aston
Ben Aston

Reputation: 55729

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();
      }
    }

Upvotes: 1

Views: 418

Answers (1)

evolon
evolon

Reputation: 1586

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

Upvotes: 2

Related Questions