componentWillReceiveProps vs getDerivedStateFromProps componentWillReceiveProps vs getDerivedStateFromProps reactjs reactjs

componentWillReceiveProps vs getDerivedStateFromProps


getDerivedStateFromProps is not a direct alternative to componentWillReceiveProps, purely because of the fact that its called after every update, whether its the change in state or change in props or re-render of parent.

However whatever is the case, simply returning the state from getDerivedStateFromProps is not the right way, you need to compare the state and props before returning the value. Else with every update the state is getting reset to props and the cycle continues

As per the docs

getDerivedStateFromProps is invoked right before calling the render method, both on the initial mount and on subsequent updates. It should return an object to update the state, or null to update nothing.

This method exists for rare use cases where the state depends on changes in props over time. For example, it might be handy for implementing a <Transition> component that compares its previous and next children to decide which of them to animate in and out.

Deriving state leads to verbose code and makes your components difficult to think about. Make sure you’re familiar with simpler alternatives:

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.

If you want to re-compute some data only when a prop changes, use a memoization helper instead.

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

P.S. Note that the arguments to getDerivedStateFromProps are props and state and not nextProps and prevProps

To get into more details,

In order to make changes based on props change, we need to store prevPropsState in state, in order to detect changes. A typical implementation would look like

static getDerivedStateFromProps(props, state) {    // Note we need to store prevPropsState to detect changes.    if (      props.myPropsState !== state.prevPropsState    ) {      return {        prevPropsState: state.myState,        myState: props.myPropsState      };    }    return null;  }


Finally, I resolved my issue. It was a painful debugging:

// Child Component// instead of this// this.props.onMyDisptach([...myPropsState])// dispatching true value since myPropsState contains only numbersthis.props.onMyDispatch([...myPropsState, true])

This is because, I have two conditions: 1) on checkbox change (component) 2) on reset button pressed (child component)

I was needing to reset the states when reset button is pressed. So, while dispatching state to the props for reset button, I used a boolean value to know it's a change from the reset. You may use anything you like but need to track that.

Now, here in the component, I found some hints to the differences between componentWillReceiveProps and getDerivedStateFromProps after debugging the console output.

// Componentstatic getDerivedStateFromProps(props, state) {    const { myPropsState: myState } = props    // if reset button is pressed    const true_myState = myState.some(id=>id===true)    // need to remove true value in the store    const filtered_myState = myState.filter(id=>id!==true)    if(true_myState) {      // we need to dispatch the changes to apply on its child component      // before we return the correct state      props.onMyDispatch([...filtered_myState])      return {        myState: filtered_myState      }    }    // obviously, we need to return null if no condition matches    return null  }

Here's what I found the results of the console output:

  • getDerivedStateFromProps logs immediately whenever props changes

  • componentWillReceiveProps logs only after child propagates props changes

  • getDerivedStateFromProps doesn't respond to the props changes ( I meant for the dispatch changes as in the example code)

  • componentWillReceiveProps responds to the props changes

  • Thus, we needed to supply the changes to child component while using getDerivedStateFromProps.

The process of pasting true value in the state I require because getDerivedStateFromProps handle all the changes unlike componentWillReceiveProps handles only the child component dispatches the changes to the props.

By the way, you may use custom property to check if it is changed and update the value if getDerivedStateFromProps but for some reason I have to tweak this technique.

There might be some confusion on my wording but I hope you'll get it.


From the react docs:

Note that this method is fired on every render, regardless of the cause. This is in contrast to UNSAFE_componentWillReceiveProps, which only fires when the parent causes a re-render and not as a result of a local setState.

You are effectively overriding your state with the current props every time after calling setState(). So when you check a box (e) => this.handleMethod(e, comp.myState) is called which is assume calls setState() to update the checked state of the checkbox. But after that getDerivedStateFromProps() will be called (before render) that reverts that change. This is why unconditionally updating state from props is considered an anti-pattern.