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.

When state is not updated setState callback should not be invoked

See original GitHub issue

Do you want to request a feature or report a bug? Feature

What is the current behavior? When null is returned in setState updater, callback is invoked. this.setState((prevState) => null, callback); Example (see console): https://stackblitz.com/edit/set-state-callback

What is the expected behavior? When state is not updated callback should not be invoked.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? ^16.0.0

What do you think about it? First of all we can consider callback what invokes after every setState. On the other side we can consider it as callback invoked only after state change. In my opinion second option is more handy because if I want to invoke callback with some job, I usually need to do something when state changed.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Apr 3, 2019

In general we recommend to avoid setState callback because it makes it too easy to write code that behaves inconsistently depending on where the update happens. Try to prefer componentDidUpdate if you can.

We don’t plan to change the setState callback semantics at this point.

1reaction
shivashpcommented, Apr 1, 2019

I believe this is more related to how you implement the setState and it’s not necessary to add feature to setState method. But for your problem you have two solutions.

Solution 1 You can ensure this.setState doesn’t execute when you have values like null or undefined by evaluating and checking the value you are going to update.

Solution 2 You can use LifeCycle method componentDidUpdate which is invoked only when there is change in state or props.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS state not updated In setState callback - Stack Overflow
However in my case, the new state is actually not updated at any point at all. Logging the list in the render function...
Read more >
Why React setState/useState does not update immediately
The answer: They're just queues · React setState callback function after state changes · Use React useEffect after state has changed · Ruben...
Read more >
React setState callback function - Code Gino
Pass a callback function to setState if it might be called multiple times and the new state needs to be calculated based on...
Read more >
setState is an Asynchronous Function - Sentry
This function will get called once the state has been updated, and the callback will receive the updated value of the state. Further...
Read more >
React setState Callback - How and when to use? | Codete Blog
setState is used for updating components, which may be performed either immediately, or delayed by React. It may wait in a queue to...
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