本文へ移動
日本語

仕組み

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.

向いている場面

  • Feeds and activity logs read from top to bottom.
  • Search results on mobile where paging buttons are awkward.

向かない場面

  • 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.

使い方

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}
    />
  )
}

プロパティ

プロパティ型既定値説明
itemsT[]—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.
hasMorebooleantruefalse shows the end mark.
loadingbooleanfalseShows skeleton rows and sets aria-busy.
errorboolean | string | nullnullShows the inline error; a string replaces the default message. Loading pauses until Retry.
onRetry() => void—Called by Retry; defaults to onLoadMore.
thresholdnumber240How many px before the end to start loading.
heightnumber | 0420Maximum height of the scroll area; 0 uses the page scroll instead.
skeletonCountnumber3Skeleton rows while loading; each fades a little more.
renderSkeleton(index) => ReactNode—Custom skeleton row to match your own row layout.
labelstring—Accessible name of the list and scroll region.
labelsobject—Replaces built-in text: loading, end, error, retry, loadMore.

アクセシビリティ

  • 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.

モーション

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.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

InfiniteList.jsxinfinite-list.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

リスト
「アーチ型カルーセル」は Pro に含まれます。解除するとプレビューが表示されます。
「円柱カルーセル」は Pro に含まれます。解除するとプレビューが表示されます。