IntersectionObserver infinite scroll hook

9825
0

Scroll listeners are easy to get wrong and they can cause performance issues on long lists. IntersectionObserver lets the browser do the hard work: I put a sentinel div at the bottom of the list and trigger fetchNextPage when it becomes visible. I also guard against multiple triggers by checking isFetchingNextPage. rootMargin is a great tool—loading a little early avoids the ‘hit the bottom and wait’ feeling. This hook keeps component logic simple and reusable, and paired with cursor pagination on the backend it stays reliable even when new items are inserted while the user is scrolling.