Tutorial

Infinite Scroll Pagination

Load more as the user scrolls with an IntersectionObserver sentinel, keep the query key stable, and avoid fetching the same page twice on overlapping events.

On this page

  1. Introduction
  2. Sentinel element
  3. Observe and load
  4. Loading guard
  5. Stable key
  6. Restore on back
  7. Test the trigger
  8. Sources

Introduction

This guide covers infinite scroll pagination, a recurring concern in the data-fetching scenario. Infinite scroll feels magic but is easy to get wrong: fetching the same page twice on overlapping events, losing the scroll position on navigation, or never loading because the sentinel is below the fold. A sentinel observed by IntersectionObserver with a stable page key and a loading guard fixes all three.

Sentinel element

Put an empty div at the end of the list and observe it, so you know when the user has reached the bottom without measuring scroll math.

The trade-off worth naming is that put an empty div at the end of the list and observe it, so you know when the user has reached the bottom without measuring scroll math adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

<ul>{items.map(i => <li key={i.id}>{i.name}</li>)}</ul>
<div ref={sentinel} />

Observe and load

When the sentinel intersects, increment the page and fetch, which is the trigger; the observer fires once per intersection so you do not need a throttle.

The trade-off worth naming is that when the sentinel intersects, increment the page and fetch, which is the trigger; the observer fires once per intersection so you do not need a throttle adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

const io = new IntersectionObserver(([e]) => {
  if (e.isIntersecting && !loading && hasMore) loadMore();
});
io.observe(sentinel.current);

Loading guard

Keep a loading flag so overlapping intersections do not fetch the same page twice, which is the classic infinite scroll bug.

The trade-off worth naming is that keep a loading flag so overlapping intersections do not fetch the same page twice, which is the classic infinite scroll bug adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Stable key

Keep the query key stable across pages so the cache appends rather than replaces, which preserves earlier pages and the scroll position.

The trade-off worth naming is that keep the query key stable across pages so the cache appends rather than replaces, which preserves earlier pages and the scroll position adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Restore on back

Cache the scroll offset and pages per key and restore on mount, so back navigation does not reset the user to page one.

The trade-off worth naming is that cache the scroll offset and pages per key and restore on mount, so back navigation does not reset the user to page one adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test the trigger

Scroll to the bottom and assert the next page loads, scroll again before it settles and assert no duplicate fetch, so the guard works.

The trade-off worth naming is that scroll to the bottom and assert the next page loads, scroll again before it settles and assert no duplicate fetch, so the guard works adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: IntersectionObserver MDN, SWR infinite.

FAQ

IntersectionObserver or scroll handler?
IntersectionObserver; a scroll handler fires on every pixel and must be throttled, while the observer fires only when the sentinel enters the viewport, which is cheaper and simpler.
How do I keep scroll position on back?
Cache the scroll offset and the loaded pages per key, and restore them on mount, so back does not drop the user back to page one.

Related Examples