Cómo funciona
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.
Úsalo para
- Feeds and activity logs read from top to bottom.
- Search results on mobile where paging buttons are awkward.
Busca otra opción cuando
- 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.
Uso
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}
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
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. |
Accesibilidad
- 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.
Movimiento
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.
Añádelo a tu proyecto
Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.
InfiniteList.jsxinfinite-list.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.



