Skip to content

Bug: State updates are reordered across await in an effect in React 18. #30890

Closed
@Dakota-Smith-FL

Description

@Dakota-Smith-FL

React version: 18.3.1

Steps To Reproduce

  1. Create an effect of the following form:
useEffect(() => {
    (async () => {
        setStateA("foo");
        await someAsyncFunctionThatReturnsImmediately();
        setStateA("bar");
    })();
});

Link to code example: https://codesandbox.io/p/sandbox/react-dev-forked-9m77xd

The current behavior

React will render with the second state update first ("bar"), and then re-render with the first update ("foo"). This only happens when the awaited function returns immediately. If the awaited promise resolves at a later time, the state is updated in the expected order.

In the example, you can observe this happening by clicking the button and looking at the console ("BUG RENDERED!!!😱😱😱"), or by seeing a brief flash of red on the component as it renders inaccurately. The console message triggers every time, the red flash only occurs occasionally. In our actual app we're seeing the render occur every time.

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

The expected behavior

The state updates should be applied in the order as specified in the code. This was the behavior in React 16, which can be tested in the sandbox.

This appears to be fixed in React 19.0.0-rc.0.

Previously reported in:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions