Read a vanilla store in React
A component consumes a store made with createStore rather than a bound React hook.
Zustand · Function
useStoreWithEqualityFn connects a vanilla Zustand store to React while allowing a custom equality check. It is useful when a component needs selected store data and finer control over re-rendering.
This hook connects a vanilla Zustand store to a React component. It applies the selector to obtain the value exposed to the component, then uses the optional equality function to decide whether a changed selection warrants a re-render.
useStoreWithEqualityFn<T, U = T>(store: StoreApi<T>, selectorFn: (state: T) => U, equalityFn?: (a: U, b: U) => boolean): U| Name | Type | Description |
|---|---|---|
storeApi | StoreApi<T> | The vanilla store API instance to read and subscribe to. |
selectorFn | (state: T) => U | Selects and returns data derived from the current store state. |
equalityFn | (a: U, b: U) => boolean | Optionally compares selected values; equal values can skip re-renders. |
The selected data derived from the current store state; the equality function can determine whether a re-render is skipped.
import { createStore } from 'zustand/vanilla'
import { useStoreWithEqualityFn } from 'zustand/traditional'
import { shallow } from 'zustand/shallow'
type State = { bears: number; fish: number }
const store = createStore<State>(() => ({ bears: 0, fish: 0 }))
export function Counts() {
const counts = useStoreWithEqualityFn(
store,
(state) => ({ bears: state.bears, fish: state.fish }),
shallow,
)
return <div>{counts.bears} bears, {counts.fish} fish</div>
}Reads a selected object from a vanilla store and uses shallow comparison to avoid re-rendering when its selected values are unchanged.
A component consumes a store made with createStore rather than a bound React hook.
A selector returns an object containing several fields and should avoid updates when those fields are unchanged.
A frequently rendered component should update only when its selected data changes according to domain-specific comparison rules.
It is a React Hook for consuming a vanilla Zustand store. Compared with useStore, it additionally accepts an equality function, which can prevent re-renders when the selected result is considered equal.
Follow-up: Why select a smaller value rather than the whole state? A narrower selection reduces the set of changes that can affect the component and makes equality checks more targeted.
The store argument is the StoreApi instance, selectorFn maps the current state to the value the component needs, and equalityFn compares successive selected values. The equality function is optional, so selection can still be used without a custom comparison.
Follow-up: What should an equality function compare? It should compare the selected values, not unrelated store state, and return true when the component can keep its existing rendered value.
The hook returns the selector's current result. A component skips a re-render when the equality function reports that the previous and next selected results are equal.
Follow-up: How can a component select multiple fields as one object? Return an object from the selector and use an appropriate comparator such as shallow when that matches the desired comparison semantics.
useStorecreateWithEqualityFncreateStoreshallow