Returning null from getDerivedStateFromProps is causing updates
See original GitHub issueDo you want to request a feature or report a bug?
Bug
What is the current behavior?
Returning null
from getDerivedStateFromProps
triggers updates and componentDidUpdate
is being called.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
https://jsfiddle.net/69z2wepo/328290/
What is the expected behavior?
componentDidUpdate
and render
methods should not be called.
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
In React 16.7, after setState call, getDerivedStateFromProps ...
Even if the method is called after setState, if getDerivedStateFromProps return null the state will be updated with the data inside the ...
Read more >You Probably Don't Need Derived State – React Blog
getDerivedStateFromProps exists for only one purpose. It enables a component to update its internal state as the result of changes in props. Our ......
Read more >1- shouldComponentUpdate(nextProps, nextState) - HackMD
getDerivedStateFromProps() returns an object to update state in response to prop changes. It can return a null if there is no change to...
Read more >Return values of React lifecycle methods and API callback ...
If an invalid value is returned from a React lifecycle method or callback function, it may cause a problem when React uses the...
Read more >FrontEndSessions: React Component LifeCycles - Medium
If we don't need to make an update, we just return null. ... };static getDerivedStateFromProps (props, state) { ... Cause For Concern:
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
At the very least, the documentation should mention that this is intentional and “update nothing” doesn’t mean that
componentDidUpdate
andrender
won’t be called. It is very confusing when the method specifically asks us to returnnull
in case of no updates.The state is left unchanged when you return null. The reason you return null, is that it’s a clear signal to react that you intend to not update state, whereas undefined could indicate you do want to update but forgot to return anything. Components should continue to update tho as render may depend on new props even if state doesn’t. If you want to prevent updates shouldComponentUpdate is the hook for that.