Skip to Content
ohooks

usePagination

Computes pagination state, page ranges with ellipsis markers, data slice indices, and navigation helpers.

Loading preview...

The usePagination hook calculates total pages, [startIndex, endIndex) array slice bounds, and a UI-ready items array containing page numbers and 'ellipsis' markers, while supporting both controlled and uncontrolled current page state.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/usePagination.json

Usage

Examples

Rendering a Pagination Bar with Ellipsis

Map over items to render numbered page buttons alongside 'ellipsis' separators.

Slicing Client-Side Data

Use startIndex and endIndex directly with Array.prototype.slice to paginate an in-memory array.

API Reference

UsePaginationOptions

Configuration options for item counts, page size, and sibling/boundary ranges.

Property
Specification
totalItems?
number
Total number of items across all pages (used with pageSize when totalPages is not set)
pageSize?
numberdefault:10
Number of items per page
totalPages?
number
Explicit total number of pages (overrides totalItems / pageSize calculation)
page?
number
Controlled current page number (1-indexed)
defaultPage?
numberdefault:1
Initial page number in uncontrolled mode
siblingCount?
numberdefault:1
Number of sibling pages shown on each side of the current page
boundaryCount?
numberdefault:1
Number of boundary pages always shown at the start and end
onChange?
function
Callback fired when the active page changes

UsePaginationReturn

Computed pagination state, slice indices, and navigation methods returned by the hook.

Property
Specification
page
number
Current active page number (1-indexed)
totalPages
number
Total number of pages
items
array
Sequence of page numbers and 'ellipsis' markers for rendering pagination controls
hasPrevPage
union
Whether a previous page exists (page > 1)
hasNextPage
union
Whether a next page exists (page < totalPages)
startIndex
number
Zero-based start index for slicing the current page's data
endIndex
number
Exclusive end index for slicing the current page's data
setPage
function
Navigates to a specific page number (clamped to [1, totalPages])
nextPage
function
Advances to the next page
prevPage
function
Moves back to the previous page
firstPage
function
Jumps to page 1
lastPage
function
Jumps to totalPages