You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instead of using setCounter(counter + 1); use functional update of setCounter like this: setCounter(prevCounter => prevCounter + 1);. In this case prevCounter will contain the latest value. Look here
bvaughn
changed the title
Bug: When waiting for an async action to end, and in the meantime the state changes, there is no way to know about the change
When waiting for an async action to end, and in the meantime the state changes, there is no way to know about the change
Jul 7, 2020
React version:
16.13
Steps To Reproduce
Link to code example:
https://codesandbox.io/s/red-bush-7w8yk?file=/src/App.js
-- A different version of the same issue
https://stackoverflow.com/questions/62424530/how-to-get-the-changed-state-after-an-async-action-using-react-functional-hooks
The current behavior
current behaviour: the counter value at the end of the async action will equal to 1
The expected behavior
expected behaviour: the counter value at the end of the async action will equal to 2
The text was updated successfully, but these errors were encountered: