Web · Components
InfiniteScroll
Wrapper that loads the next page when the end of the list is reached: feeds, product lists, comments, chat history.
5 exports
Live example
Preview
Post 1
Post 2
Post 3
Post 4
Post 5
Post 6
Post 7
Post 8
Post 9
Post 10
Code
Events
- Events appear here when you interact with the preview.
Import
Wrapper that loads the next page when the end of the list is reached: feeds, product lists, comments, chat history. Give the items as children; for tens of thousands of items use VirtualList (it has the same onLoadMore behavior).
- It doesn't listen to scroll events; it watches for nearing the end with
IntersectionObserverand starts loadingthresholdahead. - One load at a time; if the page doesn't fill the screen, the next one loads too.
- On error it doesn't loop; a "Try again" button appears.
direction="up": the scroll position is kept for items added at the start.manualAfter: switches to a button after a few automatic loads; there is a "more" path for keyboard and screen reader users too.aria-busyis reported while loading.
Examples
useLoadMore
functionHook that loads the next page when nearing the end of the list; the foundation of InfiniteScroll and VirtualList. Pass the returned sentinel to the ref of an empty element at the end of the list.
- It doesn't listen to scroll events; it uses
IntersectionObserver. - One load at a time; if the list still doesn't fill the screen when a load finishes, the next page loads too.
Examples
InfiniteScrollProps
interfaceProps of the InfiniteScroll component.
Extends: Omit<ComponentProps<"div">, "children">
| Name | Type | Default | Description |
|---|---|---|---|
children? | ReactNode | Items loaded so far. | |
onLoadMore | () => unknown | Loads the next page. If it returns a Promise, it isn't called again until it settles; if it throws, a "Try again" button appears. | |
onError? | (error: unknown) => void | Called when onLoadMore fails (logging, notice etc.). The error is caught and a "Try again" button is shown; it doesn't reach the console as an uncaught error. | |
hasMore | boolean | Are there more pages to load? | |
loading? | boolean | Loading state from outside (e.g. the first page); doesn't trigger while true. | |
direction? | "down" | "up" | "down" | Load direction. "up": like a chat; older items are added to the start of the list and the scroll position is kept (the content doesn't jump). |
threshold? | number | 400 | Start loading this far from the end (px). |
manualAfter? | number | After this many automatic loads it switches to a "Load more" button, so the page footer can be reached. 0 means always a button. If omitted, it always loads automatically. | |
loader? | ReactNode | A spinner | Loading indicator. |
endMessage? | ReactNode | Shown at the end of the list when everything is loaded. true shows the built-in text. | |
errorMessage? | ReactNode | {@link UIProvider} text | Text shown when loading fails. |
loadMoreLabel? | ReactNode | {@link UIProvider} text | Text of the "Load more" button. |
scrollable? | boolean | false | The list scrolls in its own box (a chat window, side panel). Give the height with className (h-96, max-h-[60vh], flex-1). The library's overlay scrollbar is used (see ScrollArea); you don't need to write overflow-y-auto. If omitted, the nearest scrollable ancestor (e.g. a ScrollArea) scrolls, or the page if none. |
scrollElement? | () => HTMLElement | null | Scroll area to observe. If omitted, the nearest scrollable ancestor, or the page if none. |
LoadMoreStatus
typeLoading state.
UseLoadMoreOptions
interfaceOptions of the useLoadMore hook.
| Name | Type | Default | Description |
|---|---|---|---|
onLoadMore | (() => unknown) | undefined | Loads the next page. If it returns a Promise, it isn't called again until it settles; if it throws (or the Promise rejects), the error is caught and the status becomes error: it doesn't reach the console/dev overlay as an uncaught error. | |
onError? | (error: unknown) => void | Called when onLoadMore fails (logging, notice etc.). The error is already caught and shown as "Try again" in the UI; don't rethrow it here. | |
hasMore | boolean | Are there more pages to load? Not observed while false. | |
root? | () => Element | null | Scroll area in which the sentinel element (the end of the list) is observed. null is the page itself. If omitted, the sentinel's nearest scrollable ancestor is found. | |
threshold? | number | 400 | Start loading this far from the end (px). |
loading? | boolean | Loading state from outside (e.g. the first page); doesn't trigger while true. | |
auto? | boolean | true | If false, the sentinel is not observed (e.g. "Load more" button mode). |