Pagination
<Pagination // Optional parameters defaultRefinement={number} showFirst={boolean} showPrevious={boolean} showNext={boolean} showLast={boolean} padding={number} totalPages={number} translations={object} />
1
import { Pagination } from 'react-instantsearch-dom';
About this widget
We released React InstantSearch Hooks, a new InstantSearch library for React. We recommend using React InstantSearch Hooks in new projects or upgrading from React InstantSearch.
The Pagination
widget displays a pagination system allowing the user to change the current page.
The Algolia search engine limits paginating to 1,000 hits per page.
Examples
1
2
3
import { Pagination } from 'react-instantsearch-dom';
<Pagination />
Parameters
defaultRefinement
|
type: number
Optional
The value of the page selected by default. |
||
Copy
|
|||
showFirst
|
type: boolean
default: true
Optional
Whether to display the first page link. |
||
Copy
|
|||
showPrevious
|
type: boolean
default: true
Optional
Whether to display the previous page link. |
||
Copy
|
|||
showNext
|
type: boolean
default: true
Optional
Whether to display the next page link. |
||
Copy
|
|||
showLast
|
type: boolean
default: false
Optional
Whether to display the last page link. |
||
Copy
|
|||
padding
|
type: number
default: 3
Optional
How many page links to display around the current page. |
||
Copy
|
|||
totalPages
|
type: number
default: Infinity
Optional
The maximum number of pages to display (and to allow navigating to). |
||
Copy
|
|||
translations
|
type: object
Optional
A mapping of keys to translation values.
|
||
Copy
|
HTML output
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
<div class="ais-Pagination">
<ul class="ais-Pagination-list">
<li class="ais-Pagination-item ais-Pagination-item--firstPage ais-Pagination-item--disabled">
<span class="ais-Pagination-link" aria-label="First">‹‹</span>
</li>
<li class="ais-Pagination-item ais-Pagination-item--previousPage ais-Pagination-item--disabled">
<span class="ais-Pagination-link" aria-label="Previous">‹</span>
</li>
<li class="ais-Pagination-item ais-Pagination-item--selected">
<a class="ais-Pagination-link" href="#">1</a>
</li>
<li class="ais-Pagination-item ais-Pagination-item--page">
<a class="ais-Pagination-link" href="#">2</a>
</li>
<li class="ais-Pagination-item ais-Pagination-item--page">
<a class="ais-Pagination-link" href="#">3</a>
</li>
<li class="ais-Pagination-item">
<a class="ais-Pagination-link" href="#">4</a>
</li>
<li class="ais-Pagination-item ais-Pagination-item--nextPage">
<a class="ais-Pagination-link" aria-label="Next" href="#">›</a>
</li>
<li class="ais-Pagination-item ais-Pagination-item--lastPage">
<a class="ais-Pagination-link" aria-label="Last" href="#">››</a>
</li>
</ul>
</div>
Customize the UI with connectPagination
If you want to create your own UI of the Pagination
widget or use another UI library, you can use connectors.
Connectors are higher-order components. They encapsulate the logic for a specific kind of widget and they provide a way to interact with the InstantSearch context.
They have an outer component API that we call exposed props, and they provide some other props to the wrapped components which are called the provided props.
It’s a 3-step process:
// 1. Create a React component
const Pagination = () => {
// return the DOM output
};
// 2. Connect the component using the connector
const CustomPagination = connectPagination(Pagination);
// 3. Use your connected widget
<CustomPagination />
Create a React component
const Pagination = ({
number currentRefinement,
number nbPages,
function refine,
function createURL,
}) => {
// return the DOM output
};
Provided Props
currentRefinement
|
type: number
The position of the current page. |
||
Copy
|
|||
nbPages
|
type: number
The total number of existing pages. |
||
Copy
|
|||
refine
|
type: function
Selects a page. |
||
Copy
|
|||
createURL
|
type: function
Generates a URL for the corresponding search state. |
||
Copy
|
Create and instantiate your connected widget
const CustomPagination = connectPagination(Pagination);
<CustomPagination
// optional parameters
defaultRefinement={number}
/>
Exposed Props
defaultRefinement
|
type: number
Optional
The value of the page selected by default. |
||
Copy
|
If SEO is critical to your search page, your custom HTML markup needs to be parsable:
- use plain
<a>
tags withhref
attributes for search engines bots to follow them, - use semantic markup with structured data when relevant, and test it.
Refer to our SEO checklist for building SEO-ready search experiences.
Full 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
import { connectPagination } from 'react-instantsearch-dom';
const Pagination = ({ currentRefinement, nbPages, refine, createURL }) => (
<ul>
{new Array(nbPages).fill(null).map((_, index) => {
const page = index + 1;
const style = {
fontWeight: currentRefinement === page ? 'bold' : '',
};
return (
<li key={index}>
<a
href={createURL(page)}
style={style}
onClick={event => {
event.preventDefault();
refine(page);
}}
>
{page}
</a>
</li>
);
})}
</ul>
);
const CustomPagination = connectPagination(Pagination);