How it works
A sentinel after the last row is watched by an IntersectionObserver with a bottom rootMargin, so the next page starts loading while there is still content to read. If a page does not fill the view, the observer fires again until it does. An error stops loading until the person presses Retry, so a failing API is not called in a loop.
Use it for
- Feeds and activity logs read from top to bottom.
- Search results on mobile where paging buttons are awkward.
Look elsewhere when
- Content people need to reach the footer of, or return to an exact position in: use pagination.
- Many thousands of rows kept in memory: use a virtual list.
Usage
import { useState } from 'react'
import { InfiniteList } from './InfiniteList.jsx'
export function Feed() {
const [items, setItems] = useState([])
const [cursor, setCursor] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
const [done, setDone] = useState(false)
const loadMore = async () => {
setLoading(true)
setError(null)
try {
const page = await fetchActivity(cursor)
setItems((list) => [...list, ...page.items])
setCursor(page.next)
setDone(!page.next)
} catch {
setError('Could not load more activity')
} finally {
setLoading(false)
}
}
return (
<InfiniteList
label="Activity"
items={items}
renderItem={(a) => <ActivityRow activity={a} />}
hasMore={!done}
loading={loading}
error={error}
onLoadMore={loadMore}
height={420}
/>
)
}Props
| Prop | Type | Default | What it does |
|---|---|---|---|
items | T[] | — | Everything loaded so far. |
renderItem | (item: T, index) => ReactNode | — | Renders one row. |
onLoadMore | () => void | — | Called when the end comes within threshold, or from the Load more button. Not called while loading, after an error or when hasMore is false. |
hasMore | boolean | true | false shows the end mark. |
loading | boolean | false | Shows skeleton rows and sets aria-busy. |
error | boolean | string | null | null | Shows the inline error; a string replaces the default message. Loading pauses until Retry. |
onRetry | () => void | — | Called by Retry; defaults to onLoadMore. |
threshold | number | 240 | How many px before the end to start loading. |
height | number | 0 | 420 | Maximum height of the scroll area; 0 uses the page scroll instead. |
skeletonCount | number | 3 | Skeleton rows while loading; each fades a little more. |
renderSkeleton | (index) => ReactNode | — | Custom skeleton row to match your own row layout. |
label | string | — | Accessible name of the list and scroll region. |
labels | object | — | Replaces built-in text: loading, end, error, retry, loadMore. |
Accessibility
- The rows are a list with aria-busy while loading; the scroll area is a focusable, labelled region so keyboard users can scroll it.
- Loading, the end and errors are announced in a polite live region.
- A visible Load more button is always offered when idle, so nobody depends on scrolling to fetch more; Retry is a real button.
- Skeletons are hidden from assistive tech. Reduced motion stops the sheen and the rise of new rows.
Motion
New rows rise 8px and fade in over 420ms ease-out. Skeleton bones carry a diagonal sheen every 1.6s. An error gives one small 380ms shake; the end mark fades in.
Add it to your project
Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.
InfiniteList.jsxinfinite-list.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.



