RefirebaseRefirebase

usePagination

new

Paginated Firestore queries with automatic cursor management.

Usage

usePagination manages the startAfter cursor automatically. Call loadMore to fetch the next page; documents accumulate in the data array.

usePagination
import { usePagination } from 'refirebase/react';

function PostList() {
  const { data, loading, loadingMore, hasMore, loadMore, error } = usePagination('posts', {
    orderBy: [{ field: 'created_at', direction: 'desc' }],
    pageSize: 20,
  });

  if (loading) return <p>Loading...</p>;

  return (
    <div>
      <ul>
        {data.map((post) => (
          <li key={post.id}>{post.title}</li>
        ))}
      </ul>

      {hasMore && (
        <button onClick={loadMore} disabled={loadingMore}>
          {loadingMore ? 'Loading...' : 'Load more'}
        </button>
      )}
    </div>
  );
}

Parameters

ParamTypeDescription
collectionName*stringFirestore collection path.
options.pageSizenumberDocuments per page.(default: 20)
options.whereWhereConditionFilter conditions.
options.orderBy{ field, direction }[]Sort order.

Return value

ParamTypeDescription
dataT[]Accumulated documents from all loaded pages.
loadingbooleanTrue during the initial load.
loadingMorebooleanTrue while loading the next page.
hasMorebooleanFalse when all pages have been loaded.
loadMore() => voidLoad the next page.
errorunknown | nullError from the last operation.