1

Since getDervivedStateFromProps is called before every render, I assume the state is guaranteed to be updated synchronously before the render (i.e. it will not get batched with other setState calls like a normal setState call potentially would?)

In other words, any updates that occur in getDerivedStateFromProps are guarantee to be reflected in the following render?

Adam Thompson
  • 3,278
  • 3
  • 22
  • 37

1 Answers1

1

from React's official documentation: https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops

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.

also:

Note that this method is fired on every render, regardless of the cause.

so I would say the answer is yes but I would avoid using it. Please check this: https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html

Nada Sadek
  • 19
  • 1
  • 2
  • it will depend on what you do in getDerivedStateFromProps for sure but it will always be called right before render. please check this example: https://codepen.io/NadaSadek/pen/rEpOde – Nada Sadek Jun 28 '19 at 08:52