React introduced new static method getDerivedStateFromProps(props, state)
which is called before every render method, but why? Calling it after prop change makes sense to me but after setState
it doesn't, maybe I am missing something.
I was creating a datePicker
Component according to my company requirement, in the component date is controlled from the prop. I have the following state in the component.
selectedDate: number;
selectedMonth: number;
selectedYear: number;
currentMonth: number;
currentYear: number;
view: string;
selected represents selected date which is derived from date prop and currentMonth
and currentYear
represents month and year in the current calendar view.
If date
from prop changes selected*
, currentMonth
and currentYear
should be changed accordingly. For that, I am using getDerivedStateFromProps
but let say user clicks on Month name which will switch calendar view to month (instead of dates name of the month will be shown), the function updates the currentMonth
for this using setState, but date the prop is same as before (containing previous month) which should, but getDerivedStateFromProps
is called and currentMonth is again as same as before instead of changing.
Right I creating an extra variable in state
to track if getDerivedStateFromProps
is called due to setState
but I don't think that's the right way.
Either I am doing something wrong or missing something or getDerivedStateFromProps
should not be called after setState
. Probably I am doing something wrong.
you have the answer in your question itself. "which is called before every render method". Whenever you do a
setState
therender
method will be called.I would suggest that you lift the state variables
currentMonth
andcurrentYear
to the parent component and pass them as prop along with other three. You can also pass the change handler as prop and call it from the child.on initial
render
-currentMonth
andcurrentYear
can be set tonull
, so that you can have the logic for showing default stuff. When someone chicks on month name, you can call thechangeHandler
from parent which will pass the new prop. Now ingetderivedstatefromprops
you no longer havecurrentMonth
andcurrentYear
as `null so you know that the month has changed.I did something like this
The way
getDerivedStateFromProps
hook works whenever the new props, setState, and forceUpdate is being received.In the version of
16.3
, React is not affectinggetDerivedStateFromProps
whenever thesetState
is being used. But they improved it in the version starting with16.4
, so whenever thesetState
is being called thegetDerivedStateFromProps
is being hooked.Here's extracted image from React lifecycle diagram:
16.3
^16.4
So, it's up to you when to hook the
getDerivedStateFromProps
by checking props and states properly. Here's an example:I also got that issue. So I set another variable to check is that prop received for the first time.
In getderivedstatefromprops
if you want to use multiple props values you need to set your if statements (or any other logic) accordingly.