-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Callback may be stale when called #8
Comments
Hello, thanks for opening an issue. I believe that the Would it be possible for you to provide a CodeSandbox link that reproduces your issue? |
Hi, the blog post did not clarify me why it needs to have that Here is CodeSandbox link https://codesandbox.io/s/react-typescript-playground-forked-09rdd?file=/src/index.tsx In CodeSandbox wait for a while and then press stop button. |
Thanks for the CodeSandbox link. This is definitely an subtle bug. After playing around with your example, I could prevent the issue by making one of the following changes:
While I'm not 100% sure, I think this has to do with the order of which React batches setState updates. Other |
The example code is just to emphasize the problem, not to represent any kind of real world application.
I don't see point delaying value assign. I can't think of real life application where we would need to wait for DOM render before we assign the value.
In my opinion this would be the best solution |
This causes that stale callback may be called:
Easy fix is to just remove useEffect and assign callback immediately:
The text was updated successfully, but these errors were encountered: