Possible issue with the behavior of getDerivedStateFromProps

(Zack Sunderland) #1

As mentioned in the documentation, getDerivedStateFromProps gDSFP “…is invoked right before calling the render method, both on the initial mount and on subsequent updates”. It is confusing that this function would run on state changes and not just on props changes. The function is static so it’s can’t reference the state instance property, and it doesn’t receive state as a parameter, so there’s no way for it to rely on state in any way. Thus running the function on state changes, but the same props, will always return the exact same result.
Beyond the lack of need, the function running on state changes is unintuitive. The name of the function implies, correctly, that the function is purely dependent on props so it running on state changes is unexpected. Unlike the lifecycle methods, whose names all express when they are ran, gDSFP runs at a time which is counter-intuitive to it’s name.