Skip to main content

Infinite Scroll

The InfiniteScroll component provides a reusable pattern for loading paginated data as the user scrolls.

Overview

This component wraps any list of items and automatically triggers loading of the next page when the user scrolls near the bottom. It includes:

  • Intersection Observer for efficient scroll detection
  • Loading states and error handling
  • Keyboard-accessible "Load more" button as fallback
  • Support for any data type via render props

Usage

import { InfiniteScroll } from '@krakentech/blueprint-utils/client';

function BillsList() {
const {
data,
fetchNextPage,
hasNextPage,
isFetching,
error
} = useBills({ accountNumber: 'A-12345', first: 10 });

return (
<InfiniteScroll
data={data}
error={error}
hasNextPage={hasNextPage}
isFetching={isFetching}
loadFn={fetchNextPage}
loadMoreText="Load more bills"
RenderComponent={({ data }) => (
<ul>
{data.map(bill => (
<li key={bill.id}>{bill.title}</li>
))}
</ul>
)}
/>
);
}

Props

PropTypeDescription
dataT[]The array of items to render
errorError | nullAny error from fetching
hasNextPagebooleanWhether more pages are available
isFetchingbooleanLoading state
loadFn() => voidFunction to load next page
loadMoreTextstringText for the fallback button
RenderComponentReact.FC<{ data: T[] }>Component to render the items

Accessibility

The component includes a visible "Load more" button that appears briefly when scrolling. This ensures keyboard users can navigate and trigger pagination without relying on scroll events.

Example

And here is an example of this component in action. If you scroll to the bottom, a 'Load more' button is briefly visible. This button is provided to allow for keyboard accessibility. If you open the developer tools for the page, you'll also see the mocked request trigger in the network tab.

View Full Story →