Skip to Content
ohooks

useInfiniteScroll

Triggers an asynchronous onLoadMore callback when a sentinel element scrolls into view using IntersectionObserver.

Loading preview...

The useInfiniteScroll hook observes a sentinel element with IntersectionObserver, invokes onLoadMore when it enters the viewport (with configurable distance pre-fetching), and prevents duplicate concurrent calls while a page is loading.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useInfiniteScroll.json

Usage

Examples

Paginated Feed with a Bottom Sentinel

Place an empty sentinel element at the bottom of your list and pass its ref to useInfiniteScroll.

Reverse Infinite Scroll for Chat History

Set direction: "top" and provide a scrollable container root ref to load older messages when the user scrolls near the top.

API Reference

UseInfiniteScrollOptions

Configuration options for intersection root, pre-fetch distance, and pagination state.

Property
Specification
root?
uniondefault:browser viewport
Scrollable root container element or ref
rootMargin?
stringdefault:'0px 0px 200px 0px'
Margin around the root element used to trigger loading early
threshold?
uniondefault:0
Intersection ratio threshold(s) to trigger onLoadMore
hasMore?
uniondefault:true
Whether more items are available to load
enabled?
uniondefault:true
Whether the intersection observer is active

UseInfiniteScrollReturn

Loading status and error state returned by the hook.

Property
Specification
isLoading
union
Whether onLoadMore is currently executing
loadMore
function
Manually triggers onLoadMore if not already loading and hasMore is true