Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Avoid a fresh default array/object in a hook dependency
Defaulting to [] or {} inside render creates a new reference each render, defeating memoization. Use optional chaining with ?? in the body instead.
Bad example
| 1 | const list = data[key] || []; |
| 2 | const years = useMemo(() => list.map(i => i.year), [list]); // list is new every render |
Explanation (EN)
A fresh fallback array invalidates the memo on every render while data is absent.
Objašnjenje (HR)
Good example
| 1 | const list = data[key]; |
| 2 | const years = useMemo(() => list?.map(item => item.year) ?? [], [list]); |
Explanation (EN)
The optional collection itself is the dependency; the mapping order is preserved without adding an unrelated sort.
Objašnjenje (HR)
Notes (EN)
When memoization depends on an optional collection, avoid allocating a fallback before the dependency comparison; keep equivalent mapping and ordering semantics.
Exceptions / Tradeoffs (EN)
A new empty array is harmless when no identity-sensitive consumer exists. A stable shared empty array must not be mutated.