Sub topic 5
Controlling re-renders and using useShallow
This chapter assumes you can write React function components and hooks, and that you have created a Zustand store and read from it with a selector. Here you will see exactly when a component re-renders and how to select several values without extra renders.
How Zustand decides to re-render
When the store changes, Zustand runs each mounted component's selector on the new state. It compares the result with the previous result using Object.is. If the two values are the same, the component does not re-render. If they differ, it does.
This works well for primitives such as numbers and strings. It also works for references that stay stable, such as a function stored in state.
import { useCounterStore } from './counterStore'
function Count() {
const count = useCounterStore((state) => state.count)
return <p>Count: {count}</p>
}
function Controls() {
const increment = useCounterStore((state) => state.increment)
return <button onClick={increment}>Add one</button>
}Controls selects only increment. The function is created once, so its reference never changes. Clicking the button changes count, which re-renders Count but not Controls.
The problem with object selectors
A selector that builds a new object returns a different reference every time it runs, even when the values inside are the same. Object.is sees a new value on every store update, so the component re-renders after every change to any part of the store.
// Re-renders on every store update
const { name, email } = useUserStore((state) => ({
name: state.name,
email: state.email,
}))Selecting the whole store with useUserStore() has the same effect, because the component then depends on all of the state.
Fixing it with useShallow
useShallow wraps your selector. It compares the result's top-level fields one by one instead of comparing the object reference. If every field is equal, it reuses the previous result, and the component does not re-render.
import { useShallow } from 'zustand/react/shallow'
import { useUserStore } from './userStore'
function Profile() {
const { name, email } = useUserStore(
useShallow((state) => ({ name: state.name, email: state.email })),
)
return <p>{name} ({email})</p>
}Now Profile re-renders only when name or email changes. Changes to other fields in the store are ignored.
The comparison is shallow. It checks the fields of the returned object or array, but it does not look inside them. If one field holds a new array on each run, such as the result of .filter(), the comparison still sees a change.
Choosing between the options
| Approach | What it returns | Re-renders when |
|---|---|---|
| One selector per value | A single value | That value changes |
| Object selector without `useShallow` | A new object each time | Any store update |
| Object selector with `useShallow` | An object with stable contents | Any selected field changes |
Separate selectors are the simplest choice for one or two values. Use useShallow when several related values are easier to read as a group.
Exercise
A component calls useCartStore((state) => ({ items: state.items, total: state.items.length })) and re-renders whenever any part of the store changes. Why does this happen, and how do you fix it?
Show answer
The selector returns a new object on every run, so Object.is sees a different value after each store update. Wrap the selector in useShallow, which compares items and total field by field. The component then re-renders only when one of them changes. Alternatively, use two separate selectors, one for items and one for the length.