Skip to content

[Suggestion] Document the usage of flushSync with createRoot.render for synchronous rendering #7317

Closed
@jukben

Description

@jukben

Summary

Document the usage of the flushSync API with render to achieve synchronous initial rendering, effectively mimicking pre-Concurrent Mode behavior.

Page

https://react.dev/reference/react-dom/client/createRoot

Details

While integrating React Router (using RouterProvider), we ran into an issue. After moving most of our routes to use loader for the fetch-as-you-render pattern, we noticed that some routes with very simple or fast loaders were breaking the app.

The problem comes down to a timing issue (this was tricky to debug since it only happened in the production build and only when the console wasn’t open—though I’m not sure why, and it might be a red herring). React Router subscribes to its internal state in a useLayoutEffect (relevant code), but if the loaders finish before this subscription happens, the root app component doesn’t get the update and can’t proceed with rendering.

While looking for a way to flush updates synchronously (shoutout to this Stack Overflow post), I learned that the flushSync API can handle this (I never though about using this API outside of React, I mean before initial render...).

Here’s a sandbox example showing how it behaves.

I think this behavior should be mentioned on react.dev (in the createRoot section?). It’s a key detail for anyone needing synchronous rendering in cases like this (similar to how React worked before Concurrent Mode).

I’m also not sure if React Router’s current approach is the best way to handle this. I’ll open an issue with them as well to suggest adding something about this edge case in their documentation.

Happy to help push this forward—just let me know the next steps!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions