<Pagination>
<Pagination // Optional props totalPages={number} padding={number} showFirst={boolean} showPrevious={boolean} showNext={boolean} showLast={boolean} classNames={Partial<PaginationClassNames>} ...props={React.ComponentProps<'div'>} />
About this widget # A
<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 # A
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-hooks-web';
const searchClient = algoliasearch('AJ0P3S7DWQ', '90dfaaf5755e694f341fe68f6e41a6d4');
function App() {
return (
<InstantSearch indexName="instant_search" searchClient={searchClient}>
<Pagination />
</InstantSearch>
);
}
Props # A
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
CSS classes to pass to the widget’s elements. This is useful to style widgets with class-based CSS frameworks like Bootstrap or Tailwind CSS.
|
||
Copy
|
|||
...props
# |
type: React.ComponentProps<'div'>
Optional
Any |
||
Copy
|
Hook# A
React InstantSearch Hooks 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-hooks-web';
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 with useCallback()
to avoid rendering endlessly. Objects and arrays are memoized so you don’t have to stabilize them.
APIs#
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.
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
import React from 'react';
import { usePagination } from 'react-instantsearch-hooks-web';
function CustomPagination(props) {
const { pages, refine } = usePagination(props);
return <>{/* Your JSX */}</>;
}