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 }));

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.

Further reading

Problem with a question

State Management · Middle · question 21

Test yourself

Take the State Management · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz