How the infinite scroll hook works
mainThe hook works by using an IntersectionObserver to monitor a "sentry" component. When this sentry component becomes visible (or approaches visibility based on rootMargin), the onLoadMore callback is triggered.
Key Concepts:
- Sentry Component: A component (like a loading indicator, an empty
div, or the last item in a list) that must remain mounted as long as you want the observer to stay active. To prevent flickering and maintain layout consistency, it is recommended to keep the sentry mounted even if yourloadingstate isfalse, provided there is still a next page to load. - Scrolling Direction: You can place the sentry at the bottom for vertical scrolling, at the top for chat-like interfaces, or use it for horizontal scrolling.
- Browser Compatibility: The hook relies on
IntersectionObserver. For older browsers, you may need to provide a polyfill.