Centralized application state
A React application needs shared state without provider or reducer boilerplate.
Zustand · Const
Zustand recommends a Flux-inspired organization: keep global state in one store, update it through set or setState, and colocate actions with state. Large stores can use slices, while reducer-based Redux patterns remain optional.
A Flux-inspired Zustand store centralizes application state and updates it through the store's set or setState API. Actions can live beside the state, while slices can modularize a large store; Redux-style reducers and dispatch are optional rather than required.
const useStore = create((set) => ({ state: initialValue, action: () => set((state) => ({ state: nextValue })) }))import { create } from 'zustand'
type CounterStore = {
count: number
increment: (by: number) => void
}
const useCounterStore = create<CounterStore>((set) => ({
count: 0,
increment: (by) => set((state) => ({ count: state.count + by })),
}))
export function Counter() {
const count = useCounterStore((state) => state.count)
const increment = useCounterStore((state) => state.increment)
return <button onClick={() => increment(1)}>{count}</button>
}The store keeps state and its update operation together; the component selects only the values it uses.
A React application needs shared state without provider or reducer boilerplate.
A growing store contains separate domains such as users, billing, and UI state.
A team prefers explicit reducer transitions and dispatch semantics.
The recommended arrangement is one Zustand store for an application's global state. A large store may be split into slices, but those slices still compose the main store.
Follow-up: How would you retain modularity without creating unrelated global stores? Compose domain-specific slices into the single main store.
Use set or setState for store updates. These APIs merge the described update and notify listeners correctly.
Follow-up: Why avoid mutating state directly? Direct mutation bypasses the store's update and notification mechanism, so subscribed consumers may not be updated correctly.
Zustand lets actions be functions stored directly alongside state, so separate dispatched actions and reducers are not necessary. If Redux-like behavior is needed, a root-level dispatch function or the redux middleware can provide it.
Follow-up: What does the redux middleware add? It connects a reducer and initial state and supplies dispatch on the state and vanilla API.
setsetStateslicesdispatchredux middleware