useIntersectionObserver hook
Track element visibility for lazy loading, infinite scroll and scroll-spy navigation.
- #react
- #hooks
- #performance
- #intersection-observer
Reports whether a ref'd element intersects the viewport, using one IntersectionObserver instead of scroll math.
Overview
A scroll handler forces a layout read on every frame. IntersectionObserver does that work off the main thread and calls back in batches, only when visibility changes.
Implementation
Infinite scroll
Put a sentinel element after the last row and fetch the next page when it becomes visible:
A rootMargin of 400px starts the request 400px before the reader reaches the bottom, which hides the request latency.
Related entries
- List virtualization budget. The alternative when the whole dataset is already in memory.
- Memoization rules. Keep the options object stable so the hook does not re-create the observer.
Related entries
useEventListener hook
Attach a DOM event listener once and keep the handler current, so a changing callback never detaches the listener.
useMediaQuery hook
Subscribe to a CSS media query from React, without a resize listener or a layout read.
useLocalStorage hook
A state hook that mirrors its value into localStorage, with safe hydration and cross-tab synchronization.