Skip to main content

Infinite list with max pages

A product list that loads more as you scroll, but never holds more than three pages. Scroll down and the top pages are dropped. Scroll back up and they are fetched again. The bar at the top shows which pages are loaded and how many requests were sent.

advanceduseInfiniteFetchpagination
import { useEffect, useRef } from "react";

/**
* Calls `onVisible` while the element is near the viewport. `watch` creates a new observer after every change,
* and a new observer reports the current state at once, so a still visible element triggers again.
*/
export const useInView = <T extends Element>(
onVisible: () => void,
{ enabled, watch }: { enabled: boolean; watch: unknown },
) => {
const ref = useRef<T>(null);
const callback = useRef(onVisible);

useEffect(() => {
callback.current = onVisible;
});

useEffect(() => {
const element = ref.current;
if (!element || !enabled) return undefined;

const observer = new IntersectionObserver(([entry]) => entry?.isIntersecting && callback.current(), {
rootMargin: "200px",
});
observer.observe(element);
return () => observer.disconnect();
}, [enabled, watch]);

return ref;
};
Run locallynpx tiged BetterTyped/hyper-fetch/examples/react/infinite-max-pages hyper-fetch-react-infinite-max-pages && cd hyper-fetch-react-infinite-max-pages && npm install && npm run dev
What it shows
  1. maxPages: 3 on useInfiniteFetch keeps the list at three pages. Loading one at one end drops one at the other.
  2. fetchPreviousPage loads the page before the first loaded one, so the list works in both directions.
  3. A spacer keeps the scroll position steady while pages above are dropped and loaded again.
  4. Live status: loaded page numbers, the visible item range and a request counter.

How it works​

  1. api.ts sets 20 products per page and describes the dummyjson paging: offset: "skip", limit: "limit", items: "products" and total: "total". With an offset, the hook can compute both the next and the previous page.

  2. App.tsx passes maxPages to the hook:

    const list = useInfiniteFetch(getProductsRequest, { pagination: productsPagination, maxPages: 3 });

    When fetchNextPage() adds a fourth page, the first one is dropped and hasPreviousPage becomes true. When fetchPreviousPage() brings it back, the last page is dropped instead.

  3. pageParams holds the skip of every loaded page. pageParams[0] is the index of the first item on screen.

  4. Dropping pages at the top would move the rest of the list up. A spacer with the height of the dropped rows (firstIndex * rowHeight) takes their place, so what you are looking at stays where it is. The rows have a fixed height for this reason.

  5. useInView watches the spacer and a sentinel below the list. When the spacer comes into view, the page before is fetched. When the sentinel comes into view, the next page is fetched. After a failed page, a Try again button calls the same function.

  6. A dropped page is fetched from the network again. The request counter goes up when you scroll back. maxPages trades requests for a smaller list, so pick a page size that is taller than the screen.

When to use it
  • Long feeds, logs or tables where people scroll far and memory or render time starts to matter.
  • Combine it with a virtualized list when rows have different heights. Then the virtualizer gives you the offsets instead of a fixed row height.

Summary

maxPages caps how many pages useInfiniteFetch keeps, and fetchPreviousPage loads the dropped ones back. A spacer with the height of the dropped rows keeps scrolling smooth in both directions.