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
reacthooksintersection-observerreuse
Abstract IntersectionObserver logic into a reusable visibility hook
Extract element-in-view detection into a hook like useElementIsVisible that returns a boolean, instead of repeating IntersectionObserver wiring inside feature hooks.
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codets
| 1 | const useInfiniteScroll = (...) => { |
| 2 | useEffect(() => { |
| 3 | const observer = new IntersectionObserver(entries => { |
| 4 | entries.forEach(entry => { if (entry.isIntersecting && shouldLoadMore) loadMore(); }); |
| 5 | }); |
| 6 | if (ref.current) observer.observe(ref.current); |
| 7 | return () => observer.disconnect(); |
| 8 | }); |
| 9 | }; |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | const isVisible = useElementIsVisible(ref, { threshold: 100, root: null }); |
| 2 | useEffect(() => { |
| 3 | if (isVisible && shouldLoadMore) loadMore(); |
| 4 | }, [isVisible]); |
Explanation (EN)
Objašnjenje (HR)