# usePagination (/docs/hooks/use-pagination)





`usePagination` is the state and the arithmetic of a pager, with nothing drawn. [`Pagination`](/docs/pagination) is built on it. Reach for it when you draw a pager of your own, or when a list paged on the client needs the slice to show.

```tsx
import { usePagination } from '@42-central/ui-react';

const { page, items, range, setPage, next, previous } = usePagination({
  count: 95,
  pageSize: 10,
});
```

Pages are 1-based throughout, as they're printed.

## A pager of your own [#a-pager-of-your-own]

`items` is the bar: each entry is a page (`{ type: 'page', value }`) or an ellipsis (`{ type: 'ellipsis', index }`, where `index` is its position, a stable key). Here it draws plain [`Button`](/docs/button)s.

<Preview code="&#x22;const { items, page, setPage, previous, next, canPrevious, canNext } = usePagination({\n  pages: 15,\n  defaultPage: 7,\n});\n\n<nav aria-label=\&#x22;Chapters\&#x22;>\n  <Button disabled={!canPrevious} onClick={previous}>Back</Button>\n  {items.map((item) =>\n    item.type === 'ellipsis' ? (\n      <span key={`e${item.index}`} aria-hidden>⋯</span>\n    ) : (\n      <Button\n        key={item.value}\n        variant={item.value === page ? 'filled' : 'subtle'}\n        aria-current={item.value === page ? 'page' : undefined}\n        onClick={() => setPage(item.value)}\n      >\n        {item.value}\n      </Button>\n    ),\n  )}\n  <Button disabled={!canNext} onClick={next}>Forward</Button>\n</nav>&#x22;">
  <UsePaginationDemo />
</Preview>

Once there are more pages than slots (`2 × siblings + 2 × boundaries + 3`), `items` keeps that length wherever the page is: an ellipsis that would hide a single page is that page instead. The bar keeps its width as you move through it.

## Controlled or not [#controlled-or-not]

Like a kit component, it takes `page` and `onPageChange` to be controlled, or `defaultPage` to start somewhere. The moves (`setPage`, `next`, `previous`, `first`, `last`) clamp to the existing pages and do nothing when they wouldn't change the page, so `onPageChange` only fires for a real move.

When the total shrinks under the current page (a filter leaves 2 pages while you're on the 5th), the returned `page` is clamped to the last page. The hook doesn't call `onPageChange` for that: it can't during render, and your state may be about to change anyway. Reset the page yourself where the filter changes.

## The range on the page [#the-range-on-the-page]

With `count`, `range` is the first and last item on the page, 1-based and inclusive: `{ from: 21, to: 30 }`. It is both the "21–30 of 95" line and, minus one at the start, the slice of a list paged on the client: `rows.slice(range.from - 1, range.to)`. With `pages` there is no item count, so `range` is `null`.

## API [#api]

<TypeTable
  type="{
  count: { type: 'number', description: 'How many items there are. With pageSize, sets the page count. Exclusive with pages.' },
  pageSize: { type: 'number', default: '10', description: 'How many items one page holds.' },
  pages: { type: 'number', description: 'How many pages there are, when you know it directly. Exclusive with count.' },
  page: { type: 'number', description: 'The current page, 1-based, when you control it.' },
  defaultPage: { type: 'number', default: '1', description: 'The page to start on when uncontrolled.' },
  onPageChange: { type: '(page: number) => void', description: 'Called with the next page when it changes.' },
  siblings: { type: 'number', default: '1', description: 'Pages visible on each side of the current one.' },
  boundaries: { type: 'number', default: '1', description: 'Pages visible at each end of the bar.' },
}"
/>

Returns:

<TypeTable
  type="{
  page: { type: 'number', required: true, description: 'The current page, clamped to [1, totalPages]. 1 when there are no pages.' },
  totalPages: { type: 'number', required: true, description: 'How many pages there are. 0 for an empty list.' },
  items: { type: 'PaginationItem[]', required: true, description: 'The pages to draw, with { type: &#x22;ellipsis&#x22;, index } wherever pages are hidden.' },
  range: { type: '{ from: number; to: number } | null', required: true, description: 'The items on the page, 1-based and inclusive. null when the total was given as pages.' },
  canPrevious: { type: 'boolean', required: true, description: 'Whether there is a page before this one.' },
  canNext: { type: 'boolean', required: true, description: 'Whether there is a page after this one.' },
  setPage: { type: '(page: number) => void', required: true, description: 'Moves to a page, clamped to the existing ones.' },
  previous: { type: '() => void', required: true, description: 'Moves to the previous page, if any.' },
  next: { type: '() => void', required: true, description: 'Moves to the next page, if any.' },
  first: { type: '() => void', required: true, description: 'Moves to the first page.' },
  last: { type: '() => void', required: true, description: 'Moves to the last page.' },
}"
/>

`paginationRange({ page, totalPages, siblings, boundaries })` is exported too: the same item list as plain numbers and `"ellipsis"`, for code outside React.
