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 ruleP2universalStack: React
reactperformanceuseStateinitialization
Use a lazy initializer for state whose initial value is expensive
Pass a function to useState when computing the initial value is costly (e.g. reading window.innerWidth), so it runs once instead of on every render.
PR: hegnar-bellsheep-web · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codets
| 1 | const [w, setW] = useState(window.innerWidth); // evaluated every render |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | const [w, setW] = useState(() => window.innerWidth); // evaluated once |
Explanation (EN)
Objašnjenje (HR)