Sub topic 3
Reading state with selectors
This chapter assumes you can write React function components and hooks, and that you have created a store with create as in the previous chapter. It covers how components read state from a Zustand store, and why the way you read it affects rendering.
What a selector is
A selector is a function that receives the whole store state and returns the piece a component needs. You pass it to the hook that create returned.
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>
}Count subscribes to count. Controls subscribes to increment. Actions are ordinary values in the state, so you select them the same way.
How a selector controls re-renders
Zustand runs the selector after every store update. It then compares the new result with the previous result using Object.is. The component re-renders only if the two results differ.
Here is what happens when increment runs:
Countselectsstate.count. The value changed from 0 to 1, so it re-renders.Controlsselectsstate.increment. The function is the same reference as before, so it does not re-render.
Common error: selecting the whole store
You can call the hook with no selector. It returns the entire state object.
const state = useCounterStore()This works, but the component now re-renders on every change to any field. A component that only needs count will also re-render when unrelated fields change. Select the specific values you need.
Common error: returning a new object
A selector that builds a new object returns a different reference on every call. The comparison always sees a change.
// Re-renders on every store update
const { name, email } = useUserStore((state) => ({
name: state.name,
email: state.email,
}))There are two fixes. The simplest is to call the hook once per value.
const name = useUserStore((state) => state.name)
const email = useUserStore((state) => state.email)When you want several values in one call, wrap the selector in useShallow. It compares the top-level fields of the returned object instead of its reference.
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. The next chapter covers useShallow in more detail.
Summary of selector choices
| Selector returns | Re-renders when | Use for |
|---|---|---|
| A primitive or stable reference | The value changes | Most cases |
| No selector (whole state) | Any field changes | Rarely |
| A new object or array each call | Every store update | Avoid |
| A new object wrapped in `useShallow` | A top-level field changes | Several values at once |
Exercise
A component calls useCartStore((state) => state.items.filter((i) => i.inStock)). It re-renders after every store update, even when the items do not change. Why, and how can you fix it?
Show answer
filter returns a new array on every call, so Object.is always sees a different reference. Select the stable state.items and filter during render, or wrap the selector in useShallow so the array elements are compared one by one.