InfiniteHits
<InfiniteHits // Optional parameters showPrevious={boolean} hitComponent={function} translations={object} cache={object} />
1
import { InfiniteHits } 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 InfiniteHits
is used to display an infinite list of results with a “Load more” button. To create an infinite scroll experience, take a look at the infinite scroll guide.
To configure the number of retrieved hits, use the HitsPerPage
widget or pass the hitsPerPage
prop to a Configure
widget.
Examples
1
2
3
import { InfiniteHits } from 'react-instantsearch-dom';
<InfiniteHits />
Props
showPrevious
|
type: boolean
default: false
Optional
Enable the button to load previous results. The button is only used if URL Sync is implemented and if the user is not on the first page. It’s possible to override this behavior by using connectors. |
||
Copy
|
|||
hitComponent
|
type: function
Optional
Renders each hit from the results. If it is not provided, the rendering defaults to displaying the hit in its JSON form. The provided component is called with a |
||
Copy
|
|||
translations
|
type: object
Optional
A mapping of keys to translation values.
|
||
Copy
|
|||
cache
|
type: object
default: in-memory cache object
Optional
The widget internally caches all loaded hits. By default, it uses its own internal in-memory cache implementation. We provide another implementation which uses sessionStorage, which is more persistent and lets you restore the scroll position when you leave and come back to the search page. You can also provide your own implementation by providing a cache object with |
||
Copy
|
HTML output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<div class="ais-InfiniteHits">
<button class="ais-InfiniteHits-loadPrevious">
Load previous
</button>
<ol class="ais-InfiniteHits-list">
<li class="ais-InfiniteHits-item">
...
</li>
<li class="ais-InfiniteHits-item">
...
</li>
<li class="ais-InfiniteHits-item">
...
</li>
</ol>
<button class="ais-InfiniteHits-loadMore">
Load more
</button>
</div>
Customize the UI with connectInfiniteHits
If you want to create your own UI of the InfiniteHits
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 InfiniteHits = () => {
// return the DOM output
};
// 2. Connect the component using the connector
const CustomInfiniteHits = connectInfiniteHits(InfiniteHits);
// 3. Use your connected widget
<CustomInfiniteHits />
Create a React component
const InfiniteHits = ({
object[] hits,
boolean hasPrevious,
boolean hasMore,
function refinePrevious,
function refineNext,
}) => {
// return the DOM output
};
Provided Props
hits
|
type: object[]
The hits that matched the search request. |
||
Copy
|
|||
hasPrevious
|
type: boolean
Whether there are previous pages to load. |
||
Copy
|
|||
hasMore
|
type: boolean
Whether there are more pages to load. |
||
Copy
|
|||
refinePrevious
|
type: function
Loads previous results. |
||
Copy
|
|||
refineNext
|
type: function
Loads more results. |
||
Copy
|
|||
insights
|
type: function
signature: (method: string, payload: object) => void
Sends insights events.
For more details on the constraints that apply to each |
||
Copy
|
Create and instantiate your connected widget
const CustomInfiniteHits = connectInfiniteHits(InfiniteHits);
<CustomInfiniteHits />
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
import { connectInfiniteHits } from 'react-instantsearch-dom';
const InfiniteHits = ({
hits,
hasPrevious,
refinePrevious,
hasMore,
refineNext,
}) => (
<div>
<button disabled={!hasPrevious} onClick={refinePrevious}>
Show previous
</button>
<ol>
{hits.map(hit => (
<li key={hit.objectID}>{hit.name}</li>
))}
</ol>
<button disabled={!hasMore} onClick={refineNext}>
Show more
</button>
</div>
);
const CustomInfiniteHits = connectInfiniteHits(InfiniteHits);
Sending Click and Conversion events
Please refer to the guide on Sending Insight Events to learn about sending events from any InstantSearch widget.