Pages of results
import { Pagination } from "@senvenli/react/navigation";
import { useState } from "react";
export default function Example() {
const [page, setPage] = useState(3);
return <Pagination page={page} pageSize={20} total={1204} onPageChange={setPage} />;
}import { Pagination } from "@senvenli/react/navigation";Examples
With a page-size choice
Changing the size goes back to the first page.import { Pagination } from "@senvenli/react/navigation";
import { useState } from "react";
export default function Example() {
const [page, setPage] = useState(1);
const [pageSize, setPageSize] = useState(25);
return (
<Pagination
page={page}
pageSize={pageSize}
total={312}
pageSizeOptions={[10, 25, 50]}
onPageChange={setPage}
onPageSizeChange={(size) => {
setPageSize(size);
setPage(1);
}}
/>
);
}Naming the items
A formatRange function writes the range sentence; format its numbers for the locale yourself.import { Pagination } from "@senvenli/react/navigation";
import { useState } from "react";
// Name the locale: a bare `toLocaleString()` would differ between the server and the browser.
const number = new Intl.NumberFormat("en-US");
export default function Example() {
const [page, setPage] = useState(3);
return (
<Pagination
page={page}
pageSize={20}
total={1204}
onPageChange={setPage}
formatRange={(from, to, total) => `${number.format(from)}–${number.format(to)} of ${number.format(total)} orders`}
/>
);
}Guidelines
When to use it
- A list or table with more results than fit one page.
Use something else when
- The rows already live in a table with paging built in. Data table
- A single Load more continues a feed. Button
Accessibility
Built in
- The current page is marked; the range is announced ("41–60 of 1,204").
API reference
Pagination
| Prop | Type | Default | Description |
|---|---|---|---|
pageRequired | number | – | 1-based. Keep it in the URL so back and refresh return to it. |
pageSizeRequired | number | – | |
totalRequired | number | – | All items across pages. |
onPageChangeRequired | (page: number) => void | – | |
onPageSizeChange | (pageSize: number) => void | – | |
pageSizeOptions | readonly number[] | [20, 50, 100] | 20, 50 and 100 by default; the current size joins the list when it is not in it. |
formatRange | (from: number, to: number, total: number) => string | – | The range sentence naming the items: (41, 60, 1204) → "41–60 of 1,204 orders". It gets the raw numbers; format them for the locale yourself (Intl.NumberFormat). |
pageRequirednumber1-based. Keep it in the URL so back and refresh return to it.
pageSizeRequirednumbertotalRequirednumberAll items across pages.
onPageChangeRequired(page: number) => voidonPageSizeChange(pageSize: number) => voidpageSizeOptionsreadonly number[]Default[20, 50, 100]20, 50 and 100 by default; the current size joins the list when it is not in it.
formatRange(from: number, to: number, total: number) => stringThe range sentence naming the items: (41, 60, 1204) → "41–60 of 1,204 orders". It gets the raw numbers; format them for the locale yourself (Intl.NumberFormat).