<Pagination>
This is the React InstantSearch v7 documentation. React InstantSearch v7 is the latest version of React InstantSearch and the stable version of React InstantSearch Hooks.
If you were using React InstantSearch v6, you can upgrade to v7.
If you were using React InstantSearch Hooks, you can still use the React InstantSearch v7 documentation, but you should check the upgrade guide for necessary changes.
If you want to keep using React InstantSearch v6, you can find the archived documentation.
<Pagination // Optional props totalPages={number} padding={number} showFirst={boolean} showPrevious={boolean} showNext={boolean} showLast={boolean} classNames={object} translations={object} ...props={ComponentProps<'div'>} />
1
import { Pagination } from 'react-instantsearch';
About this widget
<Pagination>
is a widget that displays a pagination to browse pages.
The Algolia search engine limits paginating to 1,000 hits per page.
You can also create your own UI with
usePagination()
.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
import React from 'react';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, Pagination } from 'react-instantsearch';
const searchClient = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<Pagination />
</InstantSearch>
);
}
Props
Parameter | Description | ||
---|---|---|---|
totalPages
|
type: number
Optional
The maximum number of pages to browse. |
||
Copy
|
|||
padding
|
type: number
default: 3
Optional
The number of pages to display on each side of the current page. |
||
Copy
|
|||
showFirst
|
default: true
Optional
Whether to display the first page link. |
||
Copy
|
|||
showPrevious
|
default: true
Optional
Whether to display the previous page link. |
||
Copy
|
|||
showNext
|
default: true
Optional
Whether to display the next page link. |
||
Copy
|
|||
showLast
|
default: true
Optional
Whether to display the last page link. |
||
Copy
|
|||
classNames
|
type: Partial<PaginationClassNames>
Optional
The CSS classes you can override and pass to the widget’s elements. It’s useful to style widgets with class-based CSS frameworks like Bootstrap or Tailwind CSS.
|
||
Copy
|
|||
translations
|
type: Partial<PaginationTranslations>
Optional
A mapping of keys to translation values.
|
||
Copy
|
|||
...props
|
type: React.ComponentProps<'div'>
Optional
Any |
||
Copy
|
Hook
React InstantSearch let you create your own UI for the <Pagination>
widget with usePagination()
. Hooks provide APIs to access the widget state and interact with InstantSearch.
The usePagination()
Hook accepts parameters and returns APIs.
Usage
First, create your React component:
import { usePagination } from 'react-instantsearch';
function CustomPagination(props) {
const {
pages,
currentRefinement,
nbHits,
nbPages,
isFirstPage,
isLastPage,
canRefine,
refine,
createURL,
} = usePagination(props);
return <>{/* Your JSX */}</>;
}
Then, render the widget:
<CustomPagination {...props} />
Parameters
Hooks accept parameters. You can pass them manually, or forward the props from your custom component.
When you provide a function to Hooks, make sure to pass a stable reference to avoid rendering endlessly (for example, with useCallback()
). Objects and arrays are memoized; you don’t need to stabilize them.
Parameter | Description | ||
---|---|---|---|
totalPages
|
type: number
The maximum number of pages to browse. |
||
Copy
|
|||
padding
|
type: number
default: 3
The number of pages to display on each side of the current page. |
||
Copy
|
APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.
Parameter | Description | ||
---|---|---|---|
pages
|
type: number[]
The list of pages based on the current page and |
||
Copy
|
|||
currentRefinement
|
type: number
The current page number. |
||
Copy
|
|||
nbHits
|
type: number
The number of hits (can be approximate). |
||
Copy
|
|||
nbPages
|
type: number
The number of pages for the total result set. |
||
Copy
|
|||
isFirstPage
|
type: boolean
Whether the current page is the first page. |
||
Copy
|
|||
isLastPage
|
type: boolean
Whether the current page is the last page. |
||
Copy
|
|||
canRefine
|
type: boolean
Indicates if the pagination can be refined. |
||
Copy
|
|||
refine
|
type: (page: number) => void
Sets the current page and triggers a search. |
||
Copy
|
|||
createURL
|
type: (page: number) => string
Generates a URL of the next search state. |
||
Copy
|
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React from 'react';
import { usePagination } from 'react-instantsearch';
function CustomPagination(props) {
const {
pages,
currentRefinement,
nbPages,
isFirstPage,
isLastPage,
refine,
createURL,
} = usePagination(props);
const firstPageIndex = 0;
const previousPageIndex = currentRefinement - 1;
const nextPageIndex = currentRefinement + 1;
const lastPageIndex = nbPages - 1;
return (
<ul>
<PaginationItem
isDisabled={isFirstPage}
href={createURL(firstPageIndex)}
onClick={() => refine(firstPageIndex)}
>
First
</PaginationItem>
<PaginationItem
isDisabled={isFirstPage}
href={createURL(previousPageIndex)}
onClick={() => refine(previousPageIndex)}
>
Previous
</PaginationItem>
{pages.map((page) => {
const label = page + 1;
return (
<PaginationItem
key={page}
isDisabled={false}
aria-label={`Page ${label}`}
href={createURL(page)}
onClick={() => refine(page)}
>
{label}
</PaginationItem>
);
})}
<PaginationItem
isDisabled={isLastPage}
href={createURL(nextPageIndex)}
onClick={() => refine(nextPageIndex)}
>
Next
</PaginationItem>
<PaginationItem
isDisabled={isLastPage}
href={createURL(lastPageIndex)}
onClick={() => refine(lastPageIndex)}
>
Last
</PaginationItem>
</ul>
);
}
function PaginationItem({ isDisabled, href, onClick, ...props }) {
if (isDisabled) {
return (
<li>
<span {...props} />
</li>
);
}
return (
<li>
<a
href={href}
onClick={(event) => {
if (isModifierClick(event)) {
return;
}
event.preventDefault();
onClick(event);
}}
{...props}
/>
</li>
);
}
function isModifierClick(event) {
const isMiddleClick = event.button === 1;
return Boolean(
isMiddleClick ||
event.altKey ||
event.ctrlKey ||
event.metaKey ||
event.shiftKey
);
}