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

useMutableSource and hydration #18183

Closed
bvaughn opened this issue Feb 28, 2020 · 2 comments · Fixed by #18771
Closed

useMutableSource and hydration #18183

bvaughn opened this issue Feb 28, 2020 · 2 comments · Fixed by #18771
Assignees
Labels
React Core Team Opened by a member of the React Core Team

Comments

@bvaughn
Copy link
Contributor

bvaughn commented Feb 28, 2020

Follow up to PR #18000 and RFC reactjs/rfcs#147

The new useMutableSource hook will need at least one additional API to be able to support server rendering and hydration. This API would likely be on the React root (the object returned by createRoot) and would enable mutable sources to have their versions eagerly captured before hydration begins.

How is the version used?

Currently a work-in-progress version of each mutable source is stored on the source itself. This version enables us to avoid tearing before a source has been subscribed to. This version number is lazily populated (the first time a source is read during a given render).

How will the version be used during hydration?

In the case of server rendering, this version will need to be eagerly populated for every source so that we can detect tearing between the version of the source used for the server respond, and the version we will eventually read from while hydrating on the client.

How will this work?

One way to do this would be to store an array on each React root of mutable source and version number pairs. Each time we start (or resume) hydration, we can iterate through this array and use it to initialize the work-in-progress version for each source.

Once all outstanding hydration work is finished, we can clear the array. (Although this will require an additional change as we do not currently have a way to detect that we've finished all pending hydration work.)

@jeremy-deutsch
Copy link

How would the registerMutableSourceForHydration API in #18771 work with libraries that create mutable sources during render (ex. this or this)?

(posting here so as not to clutter the PR)

@bvaughn
Copy link
Contributor Author

bvaughn commented May 8, 2020

This API wouldn't support a create-during-render pattern. You'd need to create the source at the module level. Otherwise a mutation might happen between the time when root hydration starts and when a particular component that reads from the source is hydrated.

I don't see an alternative to this though, without e.g. tightening constraints on "version" to be serializable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants