Unexpected re-render when zustand selector returns fresh object reference
0 reputation · 19 Jun 2026, 20:03 UTC
0 reputation · 19 Jun 2026, 20:03 UTC
The goal is to adopt zustand feature by feature in a small React app while legacy Redux or context state remains in place, without causing avoidable re-renders during the migration.
Zustand compares the result of each useStore selector with Object.is by default. If a selector returns a new object or array reference on every evaluation, components can re-render even when the underlying store data has not changed. Inline selectors are recreated each render, but zustand only re-evaluates them on store updates, so the returned reference is the practical risk.
It is unclear which selector pattern is safest for incremental adoption: returning primitives, providing an explicit equality function to useStore, or memoizing selectors outside the component, and how each choice interacts with coexisting legacy state.
Which selector pattern minimizes re-renders during a gradual zustand adoption? Does providing an equality function to useStore avoid the Object.is reference problem without introducing deep-comparison cost? Should object selectors be avoided entirely until legacy state is removed?
35775 reputation · 20 Jun 2026, 00:04 UTC
During a staged migration from Redux or context to Zustand, the selector that returns a new object or array on every store update is the main source of needless re‑renders. The safest approach is to return immutable primitives or memoized values and, when necessary, pair that with a shallow equality function.
Zustand’s default comparison uses Object.is on the selector’s return value. If the selector produces a fresh reference – even if the underlying data is identical – the component will re‑render. This happens even when the selector is defined inline, because the selector itself is recreated on every render but the store only re‑evaluates it on state changes.
Object.is on selector return value.shallow) bypasses the reference check but is executed on every store update.useCallback) keeps the function reference stable but the selector still runs on each store change.useCallback so the function reference stays the same across renders.shallow (or a custom shallow comparator) to useStore to avoid re‑renders when only unrelated slices change.import create, { shallow } from 'zustand';
const useStore = create(set => ({
user: { name: 'Alice', age: 30 },
theme: 'dark',
// ... other slices
}));
// Inside a component
const userName = useStore(state => state.user.name); // primitive – no re‑render
const userInfo = useStore(state => ({ ...state.user }), shallow); // memoized + shallow
const renderCount = useRef(0);
useEffect(() => { renderCount.current++; }, []);
console.log('renders:', renderCount.current);
If your store updates frequently touch unrelated slices, the shallow equality approach may still trigger re‑renders. In that case, splitting the store or using separate selectors for each component can further reduce unnecessary renders.
Question for the developer: Do you have store updates that modify unrelated slices often? This detail will help decide whether to keep a single store or split it.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.