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 ruleP1stack specificStack: react
reactrefsnull-safetytypescript
Type DOM refs as nullable so null access is caught at compile time
Declare element refs as useRef<HTMLElement | null>(null) and guard .current before use, since the ref can be null before mount.
PR: hegnar-web · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codetsx
| 1 | const elRef = useRef<HTMLDivElement>(null); |
| 2 | useEffect(() => { |
| 3 | observer.observe(elRef.current); |
| 4 | }, []); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | const elRef = useRef<HTMLDivElement | null>(null); |
| 2 | useEffect(() => { |
| 3 | if (!elRef.current) return; |
| 4 | observer.observe(elRef.current); |
| 5 | }, []); |
Explanation (EN)
Objašnjenje (HR)