Sub topic 17
Common pitfalls and when not to use Zustand
This chapter assumes you can create a store, read it with selectors, and update it with set. It covers the mistakes that cause extra renders or broken state, and the cases where another tool fits better.
Selecting the whole store
Calling the hook with no selector subscribes the component to every change in the store.
import { useUserStore } from './userStore'
function Profile() {
// Re-renders on any change, even to fields it never reads.
const state = useUserStore()
return <p>{state.name}</p>
}Select only what you need: useUserStore((state) => state.name).
Returning a new object from a selector
A selector that builds a new object on each call returns a value that is never equal to the previous one. This causes extra renders, and in some versions of Zustand it can cause an infinite render loop. Wrap the selector in useShallow so the top-level fields are compared instead.
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>
}Mutating state directly
set merges a partial object into the state at the top level. It does not copy nested objects for you. If you change a nested value in place, the reference stays the same and subscribers may not update.
// Wrong: mutates the existing array.
addItem: (item) => set((state) => { state.items.push(item); return state }),
// Right: creates a new array.
addItem: (item) => set((state) => ({ items: [...state.items, item] })),If deep updates become tedious, use the immer middleware.
Storing values you can compute
Do not keep a value in the store if you can calculate it from other state. A stored total can drift out of sync with items. Compute it in a selector or during render instead.
Creating a shared store where you need one per tree
A store made with create lives at module level. Every component, every test, and every server request shares it. On a server-rendered app this can leak one user's data into another's request. When you need separate instances, use createStore with a context provider.
Forgetting to reset between tests
State persists across tests in the same file. Restore the initial state before each test with setState(initialState, true). The true argument replaces the state instead of merging.
When not to use Zustand
| Situation | Better choice |
|---|---|
| State that comes from a server and needs caching, refetching, and invalidation | A data-fetching library such as TanStack Query |
| State used by one component or a small subtree | `useState` or `useReducer` |
| A value that rarely changes and is read by a whole tree, such as a theme | React context |
| State that belongs in the URL, such as filters or the current page | The router |
Zustand's async actions work for simple loading and error flags. They do not give you caching, request deduplication, or retries.
Exercise
A component calls const { count, name } = useStore((s) => ({ count: s.count, name: s.name })) and re-renders on every store update. Why, and how do you fix it?
Show answer
The selector returns a new object each time, so React sees a changed value even when count and name are the same. Wrap the selector in useShallow so the top-level fields are compared, or call the hook once per field.
Exercise
Your tests pass alone but fail when run together, and each one uses the same module-level store. What is the cause and the fix?
Show answer
State from one test carries into the next because the store is shared. Save getState() once and call setState(initialState, true) in beforeEach to replace the state before each test.