getDerivedStateFromProps - responding to change in props
See original GitHub issueThe documentation states
This method exists for rare use cases where the state depends on changes in props over time
However, having read through https://github.com/facebook/react/issues/12188, it appears the only way to achieve this (checking props against prevProps to respond to a change) is by copying props into state - which appears to directly contradict your article on derived state anti patterns.
To say the least I’m a bit confused - should the documentation be changed (and getDerivedStateFromProps should not be used for responding to changes in props), or should I go ahead with the anti-pattern approach of copying props to state (pls no)? Or is there something I am missing?
TIA
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
You Probably Don't Need Derived State – React Blog
A common misconception is that getDerivedStateFromProps and componentWillReceiveProps are only called when props “change”. These lifecycles are ...
Read more >react js getDerivedStateFromProps is calling continuously
In the documentation, React points out that most of the time you don't need this method to achieve state change based on prop...
Read more >React.js static getDerivedStateFromProps() - GeeksforGeeks
If props changes, then the state will also change accordingly else, getDerivedStateFromProps will return null that indicates no change in state.
Read more >Lifecycle, state, getDerivedStateFromProps and Hooks - Medium
React 16.3 introduced a new lifecycle function called getDerivedStateFromProps . This new lifecycle function is meant as replacement to ...
Read more >How to: Updating state on prop changes | by Avery Duffin
So the react developers have come up with some replacements. Each method has specific cases. getDerivedStateFromProps. This is the first ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks for providing an example! This ensures the conversation isn’t going in circles.
As written in the blog post, the preferred alternative is to reset the state with a key.
(If you don’t have an ID property, you can just increment an integer every time you change the
regionor something like this.)The mechanism you’re suggesting (“automatically reset
followRegionwhenregionchanges”) is discouraged because the notion of “change” often doesn’t really make sense here. For example, you might want to updateregionfor some unrelated reason later without triggering a state reset — but now you don’t have that option because components below assume every object identity change is a reset.If you don’t want to use the recommended
keysolution then sure, you’d need to keepprevRegion(or, better,prevRegionID) in the state. It doesn’t contradict the article — this solution is explicitly mentioned as less recommended, but plausible. It’s more verbose and usually unnecessary, but you can do it if you’d like.Hope this helps.
Although, that is an eslint issue, and is already reported here.