Observe granular state changes
A vanilla or React-backed store needs a listener for one field or nested value.
Zustand · Function
subscribeWithSelector adds selector-based subscriptions to a Zustand store. Listeners can react to changes in selected data instead of observing unrelated state updates.
The middleware wraps a state creator and augments the store's subscribe API. A subscriber supplies a selector and a listener, so the listener runs when the selected portion of state updates rather than for every unrelated store change.
subscribeWithSelector<T>(stateCreatorFn: StateCreator<T, [], []>): StateCreator<T, [['zustand/subscribeWithSelector', never]], []>| Name | Type | Description |
|---|---|---|
stateCreatorFn | StateCreator<T, [], []> | A state-creator function receiving Zustand's set function, get function, and store; it usually returns the exposed state and methods. |
A state creator function with the subscribeWithSelector middleware mutator.
import { createStore } from 'zustand/vanilla'
import { subscribeWithSelector } from 'zustand/middleware'
type PositionStore = {
position: { x: number; y: number }
setPosition: (position: { x: number; y: number }) => void
}
const store = createStore<PositionStore>()(
subscribeWithSelector((set) => ({
position: { x: 0, y: 0 },
setPosition: (position) => set({ position }),
})),
)
const unsubscribe = store.subscribe(
(state) => state.position,
(position) => console.log('position changed', position),
)
store.getState().setPosition({ x: 10, y: 20 })
unsubscribe()Creates a vanilla store whose subscription watches only the position slice and invokes the callback when that slice changes.
A vanilla or React-backed store needs a listener for one field or nested value.
A store must notify a non-React integration when selected data changes.
A large store has unrelated updates that should not trigger a particular listener.
It is a middleware that enables subscriptions to selected portions of a Zustand store. Instead of observing the entire state, a subscriber provides a selector and receives the selected value in its callback.
Follow-up: How can this support external state management? The documented pattern is to use the store's subscribe API outside React and react to selected state changes there.
Its state creator accepts the usual set, get, and store arguments and returns a state creator with the subscribeWithSelector mutator. Applying it augments the store subscription behavior without changing the state shape exposed by the creator.
Follow-up: What does the middleware return? It returns a state creator function, which is then passed into store creation.
The documented signature is subscribeWithSelector<T>(stateCreatorFn: StateCreator<T, [], []>): StateCreator<T, [['zustand/subscribeWithSelector', never]], []>. The input is a state-creator function, and the result is another state creator carrying the middleware mutator type.
Follow-up: Why does the mutator appear in the TypeScript result? It records the middleware transformation in Zustand's state-creator typing.
subscribecreateStoreStateCreatorzustand/middleware