-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
feat: getInitialState #2277
feat: getInitialState #2277
Conversation
serverState will be used by react on the first client render; this should avoid hydration mismatches when combined with the persist middleware, which can change the state between the SSR and the first CSR
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 5aaf3a8:
|
okay this doesn't work - here's the reproduction depending on the preview build: https://stackblitz.com/edit/stackblitz-starters-lxg5yy?file=app%2Fpage.tsx I've traced it down to when we're calling
the state already contains the hydrated one from localstorage. So the question is now - where do I best get access to capturing the actual initial state that was passed to debugging a bit more, it seems that the |
Yeah, middleware can modify state during initialization. const initialState = ...
const useFooStore = create(persist((set) => ({
...initialState
}), { ... }))
useFooStore.getServerState = () => initialState |
Maybe, this would be more developer friendly: import { create } from 'zustand'
const useFooStore = create(
(set) => ({ ... }),
() => ({ ... }), // for server state
) |
But both approaches would be something that must be done in user-land, right? if the persist middleware would somehow yield the serverState (or initialState), we could use that in the react adapter. I'm thinking that this should work ootb because without getServerSnapshot implemented, any state change between ssr and csr would trigger a hydration warning - this is independent of the persist middleware |
Yeah, so if we need ootb solution, vanilla store should know about react convention. It's reasonable as most Zustand users are React users. If that's the case, we can simply define optional |
Let me add a commit. |
this avoids hydration errors when state is restored from e.g. localstorage synchronously
thanks @dai-shi 🙏 I've added one more commit where the persist middleware actually sets However, I still think we should bring back setting |
this avoids hydration mismatches when updates happen to the store state between ssr and csr
I'm still getting hydration errors (latest stackblitz fork), but I don't really understand it. error is:
debugging this at the
Not really sure how this can still show a hydration error 🤔 |
if we default to `api.getState`, we will always read the client snapshot if there is no selector passed. An identity function returns its argument, which is either the snapshot (api.getState) or the server snapshot (api.getServerState)
found out what the issue is. It's because we're using Line 46 in 2d868c6
I think the selector should just be an identity function, as it gets the snapshot passed (which is either the result from I think this should fix it: a985abe 🤞 |
yep, here is a working fork without hydration errors 🎉 https://stackblitz.com/edit/stackblitz-starters-y74eij?file=app%2Fpage.tsx |
Nice. It's historical code and I have been wondering if we should use identity function. |
This PR adds a new
getInitialState
method to the store api, which will always return the initial state that was used to create the store. Thepersist
middleware also implements this method because it alters the initial state when restoring from a storage.In React, this new method can then be used for
getServerSnaphot
inuseSyncExternalStore
to avoid avoid hydration mismatches when the state changes between the SSR and the first CSR. This could happen before when using the persist middleware, but can also happen whenever the store state was changed on the client only before completing the first CSR.Related Issues or Discussions
Fixes #1174
Summary
Check List
yarn run prettier
for formatting code and docs