Replies: 3 comments 11 replies
-
I would say so, yes. The problem about creating a store on the server is that is will be shared between users. If we can't write to it, it will always be empty, so nothing would be shared. |
Beta Was this translation helpful? Give feedback.
1 reply
-
@dai-shi I'd that due to Next.js supports SSR (runtime) and SSG (build time) that's the safe way to create stores |
Beta Was this translation helpful? Give feedback.
9 replies
-
One idea: import { create } from 'zustand';
const ssrSafe = (config, isSSR = typeof window === 'undefined') => (set, get, api) => {
if (!isSSR) {
return config(set, get, api);
}
const ssrSet = () => {
throw newError('Cannot set state of Zustand store in SSR');
};
api.setState = ssrSet;
return config(ssrSet, get, api);
};
export const useFooStore = create(ssrSafe(...)); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently, our guide suggests to use React Context with Next.js.
This is to avoid misusing module state on the server (SSR).
zustand/docs/guides/nextjs.md
Lines 28 to 29 in fec9135
However, this only applies if we write to the store on the server. Is it correct?
In that case, if we can somehow prohibit updating the store on the server, is the usage of Zustand without React Context acceptable?
@dbritto-dev @charkour @TkDodo
Beta Was this translation helpful? Give feedback.
All reactions