Skip to content
Myr

docs(use-shallow): add troubleshooting entry for "Maximum update depth exceeded" (#3507)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by seunghyun · committed by GitHub · parent 04a8487 · Browse files · 1 file+32 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Danilo Britto <dbritto.dev@gmail.com>

Diff stats

1 file+32 added−1 removed

  • other:+32 added−1 removed

1 file+32 added−1 removed

modified:docs/reference/hooks/use-shallow.md+32 added-1 removed
264 unmodified lines
265re-renders when necessary, which improves overall performance.266267## Troubleshooting268269TBD269### I get "Maximum update depth exceeded" when reading multiple values from the store270271Since v5, selectors that return a new reference on every render can trigger infinite update loops because the default equality check is `Object.is`. The most common case is bundling multiple values into a single object inside the selector. The following triggers the error:272273```tsx274const { searchValue, setSearchValue } = useStore((state) => ({275 searchValue: state.searchValue,276 setSearchValue: state.setSearchValue,277}))278```279280The wrapper object is recreated on every render, so each comparison fails and the component re-subscribes in a loop.281282Wrap the selector with `useShallow` so the wrapper's properties are compared by reference instead of the wrapper itself:283284```tsx285const { searchValue, setSearchValue } = useStore(286 useShallow((state) => ({287 searchValue: state.searchValue,288 setSearchValue: state.setSearchValue,289 })),290)291```292293Alternatively, subscribe to each value with its own selector — there is no wrapper object to recreate:294295```tsx296const searchValue = useStore((state) => state.searchValue)297const setSearchValue = useStore((state) => state.setSearchValue)298```299300See [Requiring stable selector outputs](../migrations/migrating-to-v5.md#requiring-stable-selector-outputs) in the v5 migration guide for the underlying behavior change.