State Management · Middle · Question 21 of 75
Why does a selector returning a new object cause extra re-renders (or a loop in v5)?
const { a, b } = useStore((s) => ({ a: s.a, b: s.b }));- ✓Correct answer: A new object is created each time; wrap the selector with
useShallow - 2Zustand forbids selecting more than one field
- 3Objects must be selected through
getState()only
Explanation
Zustand compares selector results with Object.is. useShallow((s) => ({ a: s.a, b: s.b })) compares the fields shallowly and keeps the same reference if they're equal.