-
Notifications
You must be signed in to change notification settings - Fork 45.8k
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
Bug: useEffect runs before DOM changes are painted #29584
Comments
This is expected. When the update is the result of a user event like To fix this case, you can move the check to a timeout. See Caveats: https://react.dev/reference/react/useEffect#caveats |
Thanks @rickhanlonii for taking the time to read my issue and reply back. This is what I found in the link you referred above -
^ This is written in the caveats section of the documentation. Both points profess the idea that React prioritises browser painting before running the effects.
|
Here's a deep dive on the change reactwg/react-18#128 |
Clarified the docs in reactjs/react.dev#6910 |
@rachitiitr see this reactjs/react.dev#6207 |
React version: ^18.0.0
Steps To Reproduce
Video Link explaining the bug:
https://www.youtube.com/watch?v=v8KLOx_Mm48
Link to code example:
https://codesandbox.io/p/sandbox/tic-tac-toe-gpjn57
The current behavior
The expectation of browser painting the click first followed by the effect execution is broken.
We get an alert first, then the updated board is painted by browser.
The expected behavior
We expect the board to update first, then the effect should run which should cause the alert.
The text was updated successfully, but these errors were encountered: