Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP2stack specificStack: react
reactstatecorrectness
Re-sync state initialized from a count/length when that count changes
State seeded from a prop's length only runs its initializer once; re-sync it in an effect if the source count can change after mount.
PR: vinify-frontend · org-mining-deep-2026-06Created: Jun 17, 2026
Bad example
Old codetsx
| 1 | const [widths] = useState(() => Array.from({ length: columnCount }, () => DEFAULT)); |
| 2 | // out-of-range indices become undefined if columnCount grows |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | const [widths, setWidths] = useState(() => Array.from({ length: columnCount }, () => DEFAULT)); |
| 2 | useEffect(() => { |
| 3 | setWidths(Array.from({ length: columnCount }, () => DEFAULT)); |
| 4 | }, [columnCount]); |
Explanation (EN)
Objašnjenje (HR)