componentWillReceiveProps vs getDerivedStateFromProps


(Ritinkar Pramanik) #1

Hi

componentWillReceiveProps is being deprecated in favor of getDerivedStateFromProps. However, getDerivedStateFromProps does not provide any way of determining whether if it’s the state or it’s the props that caused the update. Moreover, it doesn’t even provide prevProps so we can compare
prevProps and nextProps ourselves.

This makes scenarios, where we must update the state to sync with props change, more tedious as we have to make sure it’s the props change that caused the update and not anything else.

Shouldn’t we have a way of determining whether the component updated because of the props or the state? Cause to me those two are fundamentally different and treating them the same in getDerivedStateFromProps creates problems that need to be manually sidestepped.

For example:

With componentWillReceiveProps

With getDerivedStateFromProps

With getDerivedStateFromProps I had to save cache the timestamp of the last props change locally in the state and compare it to the incoming props in order to make sure the state only syncs on props change.

Thanks