Firing server call to fetch data in componentWillMount life cycle method a bad practice?
And why it is better to use componentDidMount.
Firing server call to fetch data in componentWillMount life cycle method a bad practice?
And why it is better to use componentDidMount.
UPDATE - may / 2018
There is a new feature for react in a working progress called async rendering.
As of react
v16.3.2
these methods are not "safe" to use:you can read more about it in the docs.
As a general rule don't use
componentWillMount
at all (if you use the es6class
syntax). use theconstructor
method instead.This life-cycle method is good for a sync state initialization.
componentDidMount
in the other hand is good for async state manipulation.Why?
Well, when you do an async request in the
constructor
/componentWillMount
you do it beforerender
gets called, by the time the async operation has finished therender
method most probably already finished and no point to set the "initial state" at this stage is it?.I'm not sure this is your case here, but most of the cases that developers wants to initiate state asynchronously in
componentWillMount
is to avoid a secondrender
call. but you can't avoid it can you, like mentioned above,render
will fire anyway before the async operation will finish.So, the best time to call an async operation is after a
render
has called and the component mounted (you could mountnull
or an empty<div/>
) and then fetch your data, set the state and make it re-render respectively.componentDidMount
is the best place to put calls to fetch data, for two reasons:Using
componentDidMount
makes it clear that data won’t be loaded until after the initial render. You need to setup initial state properly, so you don’t getundefined
state that causes errors.If you need to render your app on the server,
componentWillMount
will be called twice(on the server and again on the client), which is probably not what you want. Putting the data loading code incomponentDidMount
will ensure that data is only fetched from the client. Generally, you should not add side effects tocomponentWillMount
.The way I understand it, one of the biggest reasons has to do with setting up the right expectations for the developers reading the code.
If we use
componentWillMount
it's tempting to think that the fetch have time to happen, then the component "did" mount, and then the first render will happen. But that it not the case. If we do an async call (like an API call with Promises), the component will actually runrender
before the fetch can return and set the component state (or change the Redux state, or what ever).If we instead use
componentDidMount
, then it's clear that the component will render at least once before you get back any data (because the component already did mount). So, by extension, it's also clear that we have to handle the initial state in a way so that the component doesn't break on the first ("empty") render.Component Mounting life cycle is
Constructor and componentWillMount both call before render() call which is responsible for page rendering.
Here State initialized is done in Constructor and api are called in componentDidMount because of async calls.
ComponentWillMount was good to initialized state before ES6 when constructor was not there. But now ComponentWillMount is good for nothing and react team is thinking it after react 17.
In addition to above, react have moved to react fiber architecture, to avoid unnecessary re-rendering and improve performance, react has decided to move away from componentWillMount, componentWillReciveProps and componentWillUpdate methods.
UPDATE: componentWillMount will soon be deprecated.
To cite @Dan Abramov
Read more here.