question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getDerivedStateFromProps - responding to change in props

See original GitHub issue

The 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:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
gaearoncommented, Aug 29, 2018

Thanks for providing an example! This ensures the conversation isn’t going in circles.

What I need is a way of setting followRegion back to true when the region passed down as props changes (this indicates new dataset)

As written in the blog post, the preferred alternative is to reset the state with a key.

<TripMap region={region} key={region.id} />

(If you don’t have an ID property, you can just increment an integer every time you change the region or something like this.)

The mechanism you’re suggesting (“automatically reset followRegion when region changes”) is discouraged because the notion of “change” often doesn’t really make sense here. For example, you might want to update region for 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 key solution then sure, you’d need to keep prevRegion (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.

2reactions
Billy-commented, Aug 29, 2018

Although, that is an eslint issue, and is already reported here.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found