This is the React InstantSearch v7 documentation.
If you’re upgrading from v6, see the upgrade guide.
If you were using React InstantSearch Hooks,
this v7 documentation applies—just check for necessary changes.
To continue using v6, you can find the archived documentation.
Signature
Import
JavaScript
About this widget
The<InfiniteHits>
widget displays a list of results with a “Show more” button at the bottom of the list.
As an alternative to this approach,
the infinite scroll guide
describes how to create an automatically scrolling infinite hits experience.
See also: Searches without results
You can also create your own UI with
useInfiniteHits
.Examples
Props
A component that renders each hit from the results.
It receives a
hit
and a sendEvent
(for insights
) prop.When not provided, the widget displays the hit as a JSON string.JavaScript
bannerComponent
((props: { banner: SearchResults['renderingContent']['widgets']['banners'][0] }) => JSX.Element) | false
A component that renders the banner data on the
renderingContent
property from the Algolia response.
It overrides the default banner rendering.When false
, the widget does not render the banner.JavaScript
Enable the button to load previous results.
JavaScript
Whether to escape HTML tags from hits string values.
JavaScript
A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results
data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements
.If you transform an attribute you’re using with the <Highlight>
widget,
you must also transform the corresponding item._highlightResult[attribute].value
.The widget caches all loaded hits.
By default, it uses its own internal in-memory cache implementation.
Alternatively, use
sessionStorage
to retain the cache even if users reload the page.You can also implement your own cache object with read
and write
methods.
This can be handy if you need to persist the data across sessions
or if you expect the cached data to grow larger than the browser’s
5 MB allowed storage.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.
root
. The widget’s root element.emptyRoot
. The root element without results.list
. The list of results.item
. The list items.loadPrevious
. The “Load previous” button.disabledLoadPrevious
. The “Load previous” button when there are no previous results to load.loadMore
. The “Load more” button.disabledLoadMore
. The “Load more” button when there are no more results to load.bannerRoot
. The root element of the banner.bannerImage
. The image element of the banner.bannerLink
. The anchor element of the banner.
JavaScript
A dictionary of translations to customize the UI text and support internationalization.
showPreviousButtonText
. The text for the “Show previous” button.showMoreButtonText
. The text for the “Show more” button.
JavaScript
Any
<div>
prop to forward to the widget’s root element.JavaScript
Hook
React InstantSearch let you create your own UI for the<InfiniteHits>
widget with useInfiniteHits
.
Hooks provide APIs to access the widget state and interact with InstantSearch.
The useInfiniteHits
Hook accepts parameters and returns APIs.
It must be used inside the <InstantSearch>
component.
Usage
First, create your React component:JavaScript
JavaScript
Parameters
Hooks accept parameters. You can either pass them manually or forward props from a custom component.When passing functions to Hooks, ensure stable references to prevent unnecessary re-renders.
Use
useCallback()
for memoization.
Arrays and objects are automatically memoized.Whether to escape HTML tags from hits string values.
JavaScript
A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results
data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements
.If you transform an attribute you’re using with the <Highlight>
widget,
you must also transform the corresponding item._highlightResult[attribute].value
.The Hook internally caches all loaded hits using its own internal in-memory cache implementation.The library provides another implementation using
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 read
and write
methods.APIs
Hooks return APIs, such as state and functions. You can use them to build your UI and interact with React InstantSearch.The matched hits returned from Algolia.This returns the combined hits for all the pages that have been requested so far.
Use Algolia’s highlighting feature
directly from the render function.
The matched hits from Algolia for the current page.Unlike the
items
parameter, this only returns the hits for the requested page.
This can be useful if you want to customize how to add the new page of hits to the existing list.You can use Algolia’s highlighting feature directly from the render function.The complete response from Algolia.It contains the
hits
, metadata about the page, the number of hits, and more.
Unless you need to access metadata, use items
instead.The banner data from the
renderingContent
property in the Algolia response.Whether the first page of hits has been reached.
Whether the last page of hits has been reached.
Loads the previous page of hits.
Loads the next page of hits.
The function to send
click
or conversion
events.The view
event is automatically sent when this hook renders hits.
Check the insights
documentation to learn more.Example
Click and conversion events
If theinsights
option is true
,
the InfiniteHits
component automatically sends a click
event with the following “shape” to the Insights API whenever users click a hit.
JSON
sendEvent
function in your hitComponent
and send a custom click
event.
JavaScript
sendEvent
function also accepts an object as a fourth argument to send directly to the Insights API.
You can use it, for example, to send special conversion
events with a subtype.
JavaScript
Use strings to represent monetary values in major currency units (for example, ‘5.45’).
This avoids floating-point rounding issues, especially when performing calculations.