Skip to content
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: State updates are being reordered across await in an effect #31306

Open
ChrisBoostwords opened this issue Oct 20, 2024 · 0 comments
Open
Labels

Comments

@ChrisBoostwords
Copy link

Summary

Steps To Reproduce :

useEffect(() => {
    (async () => {
        setStateA("foo");
        await someAsyncFunctionThatReturnsImmediately();
        setStateA("bar");
    })();
});

The current behavior :

In React, the second state update ("bar") is rendered first, followed by the first update ("foo"). This reordering occurs only when the awaited function returns immediately. If the awaited promise resolves later, the state updates in the expected order.

In the example, you can observe this by clicking the button and checking the console for the message "BUG RENDERED!!!shockshockshock", or by noticing a brief red flash on the component when it renders incorrectly. The console message appears every time, while the red flash happens occasionally. In our actual app, the incorrect render occurs consistently.

If you modify the example to call await optionalDelay(true), the state updates will be applied in order, and the component will flash green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant