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

Implement useSyncExternalStore on server #22347

Merged
merged 1 commit into from
Sep 20, 2021

Commits on Sep 20, 2021

  1. Implement useSyncExternalStore on server

    Adds a third argument called `getServerSnapshot`.
    
    On the server, React calls this one instead of the normal `getSnapshot`.
    We also call it during hydration.
    
    So it represents the snapshot that is used to generate the initial,
    server-rendered HTML. The purpose is to avoid server-client mismatches.
    What we render during hydration needs to match up exactly with what we
    render on the server.
    
    The pattern is for the server to send down a serialized copy of the
    store that was used to generate the initial HTML. On the client, React
    will call either `getSnapshot` or `getServerSnapshot` on the client as
    appropriate, depending on whether it's currently hydrating.
    
    The argument is optional for fully client rendered use cases. If the
    user does attempt to omit `getServerSnapshot`, and the hook is called
    on the server, React will abort that subtree on the server and
    revert to client rendering, up to the nearest Suspense boundary.
    
    For the userspace shim, we will need to use a heuristic (canUseDOM)
    to determine whether we are in a server environment. I'll do that in
    a follow up.
    acdlite committed Sep 20, 2021
    Configuration menu
    Copy the full SHA
    ffdf8a1 View commit details
    Browse the repository at this point in the history