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
reactrefsdomhooks
Access DOM elements through the ref object, not a captured variable
Read element.current at use time rather than caching node into a local that can go stale when the ref changes.
PR: hegnar-web · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codetsx
| 1 | const current = ref.current; |
| 2 | useEffect(() => () => observer.unobserve(current), []); // stale capture |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | useEffect(() => { |
| 2 | const node = ref.current; |
| 3 | if (node) observer.observe(node); |
| 4 | return () => observer.disconnect(); |
| 5 | }, []); |
Explanation (EN)
Objašnjenje (HR)