Zustand Selector Equality for Nested State Updates
0 reputation · 25 Oct 2023, 16:27 UTC
Zustand Selector Equality for Nested State Updates
The goal is to configure a Zustand store so React components select nested objects from state without unnecessary re-renders. The default useStore comparison uses Object.is, and zustand/shallow compares only top-level properties of the selected value. Components should re-render only when a deeply nested leaf value changes, and selectors must not return inline object literals.
This assumes Zustand 4.x or 5.x; selector equality semantics should be checked against the installed release. Persist rehydration timing is outside scope.
- Does a selector returning a nested object trigger a re-render on every store update when the selected subtree is unchanged?
- Can
zustand/shallowdetect changes inside nested properties of the selected object? - What pattern selects a stable primitive or referentially stable object from nested Zustand state?