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
reactrefsdombest-practice
Use refs instead of document.getElementById in components
Within a React component, reference DOM nodes via refs rather than looking them up by id string, which is brittle and duplicates ids.
PR: frontpage-web · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetsx
| 1 | useEffect(() => { |
| 2 | const element = document.getElementById(elementId); |
| 3 | init(element); |
| 4 | }, []); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | const ref = useRef<HTMLDivElement>(null); |
| 2 | useEffect(() => { if (ref.current) init(ref.current); }, []); |
| 3 | return <div ref={ref} />; |
Explanation (EN)
Objašnjenje (HR)