Provider-free global state
Use a shared application store directly from React components without adding a provider.
Zustand · Const
create creates a React-bound Zustand hook from a state creator. Components use selectors with the hook to subscribe to the state they need, while store API utilities are attached to the hook.
create turns a state creator into a hook with store API methods attached. Components call that hook with a selector; each component subscribes to the selected value and re-renders when that value changes, without requiring a provider.
create<T>()(stateCreatorFn: StateCreator<T, [], []>): UseBoundStore<StoreApi<T>>| Name | Type | Description |
|---|---|---|
stateCreatorFn | StateCreator<T, [], []> | A function receiving set, get, and store; it defines the store's state and actions. |
A React hook with attached setState, getState, getInitialState, and subscribe utilities. The hook accepts a selector and returns the selected current state.
create to produce one React hook that combines component subscription with store API utilities.use prefix and is created directly from a state-creator function.set merge the supplied partial object rather than replacing the complete state.set, get, or store arguments while the store is being initialized; use them after creation, such as inside actions.import { create } from 'zustand'
type BearState = {
bears: number
increasePopulation: () => void
}
const useBear = create<BearState>()((set) => ({
bears: 0,
increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
}))
export function BearCounter() {
const bears = useBear((state) => state.bears)
return <h1>{bears} bears around here...</h1>
}
export function Controls() {
const increasePopulation = useBear((state) => state.increasePopulation)
return <button onClick={increasePopulation}>one up</button>
}Creates a React-bound store and uses selectors to read state or obtain an action.
Use a shared application store directly from React components without adding a provider.
Subscribe a component to a precise field or action instead of consuming the whole store.
Read or modify store state from code that is not rendering a React component.
create function provide?create accepts a state creator and returns a React hook with store utilities attached. The hook takes a selector, allowing a component to derive the state value it needs.
Follow-up: How can a component avoid subscribing to unrelated state? Pass a selector that returns only the required field or action.
A component invokes the returned hook with a selector, such as state => state.bears. The component re-renders when the selected state changes.
Follow-up: Why select an action separately? It expresses the component's dependency narrowly and avoids selecting unrelated state.
create?The returned hook also exposes setState, getState, getInitialState, and subscribe. These utilities are attached to the hook in addition to its selector-based React usage.
Follow-up: When would a non-React caller use these utilities? It can imperatively read, update, or subscribe to the store without rendering a React component.
useStoreuseStoreWithEqualityFnuseShallowcreateStorecreateWithEqualityFn