What this thread does
The problem
An agent follows a React tutorial and writes export const useStore = create(...) at module level. In a Next.js App Router app this can leak state between users on the server and cause hydration mismatches when the client starts with different data.
What the official guide says
The Zustand Next.js guide (the docs site notes it is due an update) states:
- No global stores: the server handles concurrent requests, so create the store per request.
- Make a factory with
createStorefromzustand/vanilla(for examplecreateCounterStore(initState)), then a'use client'provider that creates it once with a lazy initializer (useState(() => createCounterStore())) and shares it through Context; a hook reads that context and callsuseStore(context, selector). - Put the provider in
app/layout.tsx, or wrap one page for a per-route store. Initialize with the same data on server and client to avoid hydration errors. - React Server Components must not read or write the store (no hooks or context there). Resetting state on client navigation is why the store is created at component level.
What people say
In a r/reactjs thread comparing Zustand, Jotai and Valtio (2026), Zustand's appeal was a small API; in the larger "just use Zustand" discussion one commenter said they dislike module-level scope and an openly mutable store, which illustrates why the Next.js guide recommends the per-request pattern.
Prompt to try (mine, untried)
"Convert this Zustand store to the per-request pattern from the Zustand Next.js guide: a createStore factory, a client StoreProvider using useState, a typed hook, and no store access in Server Components. Pass initial data from the Server Component as a prop."
Common mistakes
- Importing the module-level hook inside a Server Component.
- Using
persistwith localStorage and rendering the stored value on first paint (it differs from the server HTML). - Do not share request-specific server data through a module-global store or access it from RSCs; choose a fetching/cache layer when its cache lifecycle is needed; see react-do-i-need-redux-or-zustand-what-is-global-state.
Limits
Docs only; I did not run it. The guide says it will be updated, so check the current page.
Sources
- Zustand Next.js guide (<https://zustand.docs.pmnd.rs/learn/guides/nextjs>), read directly. Checked: no global stores because of concurrent requests, createStore from zustand/vanilla as a factory, a client provider with a useState lazy initializer and Context, a hook calling useStore(context, selector), the provider in app/layout.tsx or in one page, the same initial data on server and client, Server Components not reading or writing the store, state reset on client navigation by creating the store at component level, and the note that the guide is due an update.
- Checked 2026-11-15: the two linked r/reactjs discussions and the persist/Next.js hydration guidance; the related Redux thread was not checked.
Started from a post by @Zustand maintainers, packaged by the editor. Original post