Signature
Import
About this widget
TheinfiniteHits
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.
To configure the number of hits to show, use the hitsPerPage
widget or the configure
widget.
See also: Searches without results
Examples
JavaScript
Options
The CSS Selector or
HTMLElement
to insert the widget into.Escapes HTML entities from hits string values.
JavaScript
Enable the button to load previous results.The button is only displayed if the routing option is enabled in
instantsearch
and users aren’t on the first page.
Override this behavior with connectors.JavaScript
The CSS classes you can override:
root
. The widget’s root element.emptyRoot
. The root container without results.list
. The list of results.item
. The list item.loadPrevious
. The “Show previous” button.loadMore
. The “Show more” button.disabledLoadPrevious
. The disabled “Show previous” button.disabledLoadMore
. The disabled “Show more” button.bannerRoot
. The root element of the banner.bannerImage
. The image element of the banner.bannerLink
. The anchor element of the banner.
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 used in the highlight
widget,
you must also transform the corresponding item._highlightResult[attribute].value
.JavaScript
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.Templates
You can customize parts of a widget’s UI using the Templates API. Each template includes anhtml
function,
which you can use as a tagged template.
This function safely renders templates as HTML strings and works directly in the browser—no build step required.
For details, see Templating your UI.
The
html
function is available in InstantSearch.js version 4.46.0 or later.The template to use when there are no results. It exposes the
results
object.The template to use for each result.
This template receives an object containing a single record.
The record has a new property
__hitIndex
for the relative position of the record in the list of displayed hits.
You can use Algolia’s highlighting feature with the highlight
function, directly from the template.JavaScript
The template to use for the banner.
This template receives an object containing the
banner
data returned within the renderingContent
property from the Algolia API response and a className
string representing the CSS classes provided to the widget.
It is rendered above the results.The template to use for the “Show previous” label.
The template to use for the “Show more” label.
HTML output
HTML
Customize the UI with connectInfiniteHits
If you want to create your own UI of the infiniteHits
widget, you can use connectors.
To use connectInfiniteHits
, you can import it with the declaration relevant to how you installed InstantSearch.js.
JavaScript
Create a render function
This rendering function is called before the first search (init
lifecycle step)
and each time results come back from Algolia (render
lifecycle step).
JavaScript
Rendering options
The matched hits from the Algolia API.
This returns the combined hits for all the pages that have been requested so far.
You can use Algolia’s highlighting feature
with the
highlight
function,
directly from the connector’s render function.JavaScript
The matched hits from the Algolia API for the current page.
Unlike the
hits
parameter, this only returns the hits for the currently requested page.
This can be useful if you want to handle how the new page of hits is going to be added to the list of search results.
You can use Algolia’s highlighting feature
with the highlight
function,
directly from the connector’s render function.JavaScript
The complete response from the Algolia API.
It contains the
hits
, metadata about the page, the number of hits, and so on.
Unless you need to access metadata, use items
instead.
You should also consider the stats
widget if you want to build a widget that displays metadata about the search.JavaScript
The banner data returned within the
renderingContent
property from the Algolia API response.
It is rendered above the results by default and can be used to display promotional content.JavaScript
The function to send
click
or conversion
events.
The view
event is automatically sent when this connector renders items.eventType: 'click' | 'conversion'
hit: Hit | Hit[]
eventName: string
insights
middleware documentation.JavaScript
Indicates whether the first page of hits has been reached.
JavaScript
Indicates whether the last page of hits has been reached.
JavaScript
Loads the previous page of hits.
JavaScript
Loads the next page of hits.
JavaScript
All original widget options forwarded to the render function.
JavaScript
Create and instantiate the custom widget
First, create your custom widgets using a rendering function. Then, instantiate them with parameters. There are two kinds of parameters you can pass:- Instance parameters. Predefined options that configure Algolia’s behavior.
- Custom parameters. Parameters you define to make the widget reusable and adaptable.
renderFunction
, both instance and custom parameters are accessible through connector.widgetParams
.
JavaScript
Instance options
Escapes HTML entities from hits string values.
JavaScript
Enable the button to load previous results.
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 used in the highlight
widget,
you must also transform the corresponding item._highlightResult[attribute].value
.JavaScript
Full example
Click and conversion events
If theinsights
option is true
,
the infiniteHits
widget automatically sends a click
event with the following shape to the Insights API when a user clicks on a hit.
JSON
sendEvent
function in your item
template 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.