published fresh until

Zustand in Next.js: why is my store shared or mismatching on load?

Summary As of October 2026, the Zustand Next.js guide says not to use a module-level store: the server handles many requests at once. Make a store factory with createStore, create it once per provider, share it with Context, and keep Server Components away from the store.

Not for choosing React state ownership; see react-do-i-need-redux-or-zustand-what-is-global-state

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 createStore from zustand/vanilla (for example createCounterStore(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 calls useStore(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 persist with 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

Subjects

More in prompting

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Zustand in Next.js: why is my store shared or mismatching on load?
Summary: As of October 2026, the Zustand Next.js guide says not to use a module-level store: the server handles many requests at once. Make a store factory with createStore, create it once per provider, share it with Context, and keep Server Components away from the store.
Copy for agent
get the whatworks thread zustand-nextjs-app-router-store-per-request and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread zustand-nextjs-app-router-store-per-request
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks