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
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)Full Signature
pageSize?numberdefault:10
Number of items per page
Full Signature
totalPages?number
Explicit total number of pages (overrides
totalItems / pageSize calculation)Full Signature
page?number
Controlled current page number (
1-indexed)Full Signature
defaultPage?numberdefault:1
Initial page number in uncontrolled mode
Full Signature
siblingCount?numberdefault:1
Number of sibling pages shown on each side of the current page
Full Signature
boundaryCount?numberdefault:1
Number of boundary pages always shown at the start and end
Full Signature
onChange?function
Callback fired when the active page changes
Full Signature
Parameters
(pagenumberUsePaginationReturn
Computed pagination state, slice indices, and navigation methods returned by the hook.
Property
Specification
pagenumber
Current active page number (
1-indexed)totalPagesnumber
Total number of pages
itemsarray
Sequence of page numbers and
'ellipsis' markers for rendering pagination controlsFull Signature
hasPrevPageunion
Whether a previous page exists (
page > 1)Full Signature
hasNextPageunion
Whether a next page exists (
page < totalPages)Full Signature
startIndexnumber
Zero-based start index for slicing the current page's data
endIndexnumber
Exclusive end index for slicing the current page's data
setPagefunction
Navigates to a specific page number (clamped to
[1, totalPages])Full Signature
Parameters
pagenumbernextPagefunction
Advances to the next page
Full Signature
prevPagefunction
Moves back to the previous page
Full Signature
firstPagefunction
Jumps to page
1Full Signature
lastPagefunction
Jumps to
totalPagesFull Signature