Accepted answer

Your code is legal according to react documentation.

You also may consider to put this code inside getInitialState method as according to another react doc initializing from props is not an anti-pattern.

You also can replace several calls with one setState method call:



The only reason componentWillReceiveProps exists is to give the component an opportunity to setState. So yes, any state you set synchronously in it will be processed together with the new props.


According to this react doc , changing states inside componentWillReceiveProps is not recommended. The doc explains it very clearly.

And it also gives us alternative solutions for "fully controlled component" and "uncontrolled components", please read this react doc


As of React v16.6.3 this is considered UNSAFE and componentWillReceiveProps is marked for deprecation. The removal is planned to happen in version 17.


Using this lifecycle method often leads to bugs and inconsistencies, and for that reason it is going to be deprecated in the future.

If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.

For other use cases, follow the recommendations in this blog post about derived state.

If you used componentWillReceiveProps for re-computing some data only when a prop changes, use a memoization helper instead.

If you used componentWillReceiveProps to “reset” some state when a prop changes, consider either making a component fully controlled or fully uncontrolled with a key instead.

In very rare cases, you might want to use the getDerivedStateFromProps lifecycle as a last resort.

In your case you should use componentDidUpdate instead.

Related Query

More Query from same tag