Per-request server stores
A server handles multiple simultaneous requests and each request needs isolated state.
Zustand · Const
A store factory is a function that creates a fresh Zustand vanilla store, optionally initialized with caller-provided state. It is useful for per-request or otherwise isolated stores.
A store factory is an ordinary function that calls createStore to produce a fresh vanilla Zustand store. Each invocation can receive initialization data and returns a separate store instance exposing state-management methods.
const createCounterStore = (initState: CounterState = defaultInitState) => {
return createStore<CounterStore>()((set) => ({
...initState,
// actions
}))
}| Name | Type | Description |
|---|---|---|
initState | CounterState | Optional initial state used to initialize the newly created store; the documented example defaults it to defaultInitState. |
A new vanilla Zustand store (StoreApi<CounterState>) for each factory invocation.
createStore when the factory should return a standalone store rather than a React hook.set, get, or store arguments while the store is being initialized; use them after creation, such as inside actions.import { createStore } from 'zustand/vanilla'
type CounterState = {
count: number
increment: () => void
}
const createCounterStore = (initialCount = 0) =>
createStore<CounterState>()((set) => ({
count: initialCount,
increment: () => set((state) => ({ count: state.count + 1 })),
}))
const store = createCounterStore(2)
store.getState().increment()
console.log(store.getState().count) // 3Creates a standalone counter store from an initial value, then uses its API to update and read state.
A server handles multiple simultaneous requests and each request needs isolated state.
Several independent widgets or tests need identical store logic with separate data.
A store must be consumed by code that is not itself a React component.
createStore in a factory?A factory is useful when each consumer needs an independent store instance, such as separate requests or isolated component state. It calls createStore for every invocation and can merge supplied initial state into the store.
Follow-up: Why avoid a global store in a server application? Concurrent requests could observe or mutate shared module state, so the store must be created per request.
createStore creates a vanilla store without React bindings. The returned object provides setState, getState, getInitialState, and subscribe, so it can be used from non-React code or connected to React separately.
Follow-up: What is the React-oriented alternative? Use Zustand's create function to create a store bound to React through a hook.
The state creator receives set, get, and store, and normally returns the state and actions to expose. Updates are performed through set, while consumers can read current state with getState on the resulting store.
Follow-up: When are the creator arguments safe to use during initialization? They should not be called synchronously until the store has been created; defer usage to actions or later operations.
createStorecreateStoreApisetStategetStategetInitialStatesubscribe