Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Access DOM elements through the ref object, not a captured variable
Read the current ref for live actions, but capture the subscribed node inside effect setup so cleanup targets the same node.
Bad example
| 1 | const current = ref.current; |
| 2 | useEffect(() => () => observer.unobserve(current), []); // stale capture |
Explanation (EN)
Capturing a node before effect setup can retain the wrong target; cleanup must undo the original subscription.
Objašnjenje (HR)
Good example
| 1 | useEffect(() => { |
| 2 | const node = ref.current; |
| 3 | if (!node) return; |
| 4 | observer.observe(node); |
| 5 | return () => observer.unobserve(node); |
| 6 | }, [observer]); |
Explanation (EN)
Cleanup removes observation from the exact node used during this effect setup.
Objašnjenje (HR)
Notes (EN)
Read ref.current when a live handler needs the current node. For effect setup and cleanup, capture the exact node that was subscribed or observed inside that effect.
Exceptions / Tradeoffs (EN)
Do not read a replacement ref.current during cleanup of the old node. Handle ref target changes with an appropriate callback ref or lifecycle dependency.