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.

Calling setState with a callback as a value doesn't work

See original GitHub issue

The only thing stored in the storage is undefined, and setState will not update it.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
astoilkovcommented, Jan 5, 2021

I successfully replicated the issue, fixed it, and released a new version.

Thank you very much! This was a big oversight on my part. I also updated the test in order to test for this in the future.

0reactions
astoilkovcommented, Jan 5, 2021

I forgot to explain the problem. Calling setState(callback) doesn’t work. I updated the example to showcase the problem: https://codesandbox.io/s/silent-forest-v5tot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

setState callback not working as expected - Stack Overflow
The fix will be to move the next state change inside the callback: In this example i moved the line this.setState({ loading: false...
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 >
useControlledState setState callback does not work #2320
We have two issues in useControlledState, both in the setState callback syntax case. ... This is due to a side effect that we...
Read more >
How to Use the setState Callback in React - Upmostly
To perform an action in a React component after calling setState, such as making an AJAX request or throwing an error, we use...
Read more >
React setState calback with Functional Component - Medium
useState returns the current state and a function to update it. But this function updates the value in an asynchronous way. That means...
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