Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
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.
Bad example
| 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)
Repeated observer setup duplicates target lifecycle and cleanup logic.
Objašnjenje (HR)
Good example
| 1 | const isVisible = useElementIsVisible(ref, { threshold: 1, root: null }); |
| 2 | useEffect(() => { |
| 3 | if (isVisible && shouldLoadMore) loadMore(); |
| 4 | }, [isVisible, shouldLoadMore, loadMore]); |
Explanation (EN)
The shared hook owns observation; the feature effect declares every reactive input.
Objašnjenje (HR)
Notes (EN)
Extract observer ownership when multiple consumers need the same visibility contract; verify threshold, root, target changes and cleanup.
Exceptions / Tradeoffs (EN)
Keep feature-specific pagination guards in the feature. A single simple observer does not automatically require an abstraction.