Stan aplikacji

state/selector-stable-result

A store selector returns existing values, not a new object or array on every call; pick fields one by one or use useShallow or shallowEqual.

poważnastatycznefe-state

Dlaczego

A selector such as (s) => ({ count: s.count, user: s.user }) builds a new object each time it runs, so the store sees a "changed" result on every update. With React Redux the component re-renders after every dispatched action, wherever it happened. With Zustand 5, which compares with Object.is, the same selector can loop until React stops with "Maximum update depth exceeded".

Poprawka

Select one value per call (const count = useStore((s) => s.count)), or wrap a multi-value selector in useShallow (Zustand) or pass shallowEqual (React Redux). A derived list (s.todos.filter(...)) belongs in a memoized selector (createSelector) or is computed in the component from the raw slice.

Przykłady

// bad
const { count, inc } = useCounterStore((s) => ({ count: s.count, inc: s.inc }))
const done = useSelector((state) => state.todos.filter((t) => t.done))
// good
const [count, inc] = useCounterStore(useShallow((s) => [s.count, s.inc]))
const todos = useSelector((state) => state.todos)
const done = todos.filter((t) => t.done)

Źródła

Fakty

Identyfikator
state/selector-stable-result
Waga
poważna
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
jsx
Tylko w projektach z
zustand redux
Uczy jej skill
fe-state State and stores
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain state/selector-stable-result