Provider-free global state
A client-side React application needs shared state without wrapper components.
Zustand · Const
Zustand's React hook can be used directly in components without a provider. Selectors subscribe each component to the state it needs.
A Zustand store created with create exposes a React hook that components can call directly. Components select the state or actions they need, and Zustand re-renders consumers when the selected state changes, so a provider is unnecessary for the usual store pattern.
const useStore = create<State>((set) => ({ ... }))| Name | Type | Description |
|---|---|---|
initializer | StateCreator<T> | State creator function that defines the store's initial state and actions; the official page presents this as the argument passed to create. |
A React hook for reading selected store state and subscribing components to changes.
import { create } from 'zustand'
type CounterState = {
count: number
increment: () => void
}
const useCounterStore = create<CounterState>((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
}))
export function Counter() {
const count = useCounterStore((state) => state.count)
const increment = useCounterStore((state) => state.increment)
return <button onClick={increment}>{count}</button>
}The component reads selected state directly from the store hook, with no context or provider wrapper.
A client-side React application needs shared state without wrapper components.
Several components need focused subscriptions to different parts of one store.
A small feature needs shared state with minimal setup and boilerplate.
The store hook is usable directly inside any component, so Zustand does not require a React context provider for this pattern. A component selects the slice it consumes, and it re-renders when that selected state changes.
Follow-up: When might a provider be appropriate? Use a vanilla store with context when dependency injection, per-request isolation, or component-level initialization is required.
Create the store with Zustand's React-bound create API, then call the returned hook with a selector. The selector lets the component subscribe to only the state or action it needs.
Follow-up: What is the benefit of selecting a slice instead of reading the whole store? It limits subscriptions and avoids re-rendering for unrelated state changes.
The provider-free approach is convenient for a normal client-side application because the store is module state and the hook can be imported wherever it is needed. It avoids context-provider and reducer boilerplate.
Follow-up: Why can global module state be problematic in server-rendered applications? A store can be shared across requests, so request-specific stores should be created and passed through context instead.
createcreateStoreselectorsReact context