Calling setState with a callback as a value doesn't work
See original GitHub issueThe only thing stored in the storage is undefined
, and setState
will not update it.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
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.
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.