Ask questionsStale values for useState inside callback functions
<!-- Note: if the issue is about documentation or the website, please file it at: https://github.com/reactjs/reactjs.org/issues/new -->
Do you want to request a feature or report a bug? potential bug
What is the current behavior?
state value is stale from inside a function. The value is correct inside the body of the component, but not inside the function itself.
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:
Begin by typing
a@g which should autocomplete to
email@example.com. You will see the
actual values for the input below each.
What is the expected behavior?
It should have the correct value for
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
Answer questions aweary
I feel like that behavior is not very desirable. In the past, in most cases, when state was updated it was reflected on the next line within a function.
This behavior has been around since before Hooks and is important for a few reasons. Check out the official FAQ, which covers this question. Hopefully that clears things up!