React unexpectedly keeps an old value of a variable produced by React.useState(...). I press the button [1] and then [2]. In 3000 ms I expect to see "null" in the alerting message, but still see "42".
Wrapping handleDisplay into React.useCallback with dependency on [val] does not help.
function App() {
const [val, setVal] = React.useState(42)
const handleHide = () => {
setVal(null)
}
const handleDisplay = () => {
setTimeout(() => {
alert(val)
}, 3000)
}
return (
<div>
<p>VAL: {val ? val : 'null'}</p>
<button onClick={handleDisplay}>[1] display value with delay</button>
<button onClick={handleHide}>[2] hide immediately</button>
</div>
)
}
At the same time — value on the page (rendered via VAL: { val ? val : 'null' }) is correct, it is "null" as expected.
Any ideas about what do I do wrong, and how can I get "null" in my alert(...)?
P.S. Here is a sandbox with a live example https://codesandbox.io/s/react-usestate-bug-24ijj