Asynchronous calls in React component

11 September, 2017.

All network calls that are necessary to load data needed by the component should go inside componentDidMount()

From React docs
componentDidMount() is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request. Setting state in this method will trigger a re-rendering.

Why not inside constructor()?

Why not in componenentWillMount()?

This function is invoked immediately before mounting occurs. So, obviously this appears to be a best place to place the call to load data. However that's not the case.

Share Tweet