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.

Returning null from getDerivedStateFromProps is causing updates

See original GitHub issue

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

github_iconTop GitHub Comments

4reactions
nkprince007commented, Dec 29, 2018

At the very least, the documentation should mention that this is intentional and “update nothing” doesn’t mean that componentDidUpdate and render won’t be called. It is very confusing when the method specifically asks us to return null in case of no updates.

3reactions
jquensecommented, Dec 29, 2018

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.

Read more comments on GitHub >

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

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