You are viewing the documentation for InstantSearch.js v4.
To upgrade from v3, see the migration guide.
Looking for the v3 version of this page?
View the v3 docs.
Signature
Import
About this widget
Theinstantsearch
object is the root wrapper component for all widgets.
Two parameters are required:
indexName
. Your main search indexsearchClient
. The search client for InstantSearch.js. The search client needs anappId
and anapiKey
, which you can find in the Algolia dashboard.
Middleware
InstantSearch.js provides middleware to help you connect to other systems:- Insights. Use the
insights
middleware to send click and conversion events - Generic. With the
middleware
API, you can inject logic into InstantSearch.js. For example, to send events to Google Analytics.
Examples
JavaScript
Options
The main index to search into.
JavaScript
Provides a search client to
instantsearch
.
Read the custom backend guidance on implementing a custom search client.JavaScript
The locale used to display numbers.
This is passed to
Number.prototype.toLocaleString()
.JavaScript
Use
onStateChange
instead.This option lets you take control of search behavior.
For example, to avoid doing searches at page load.When this option is set, search.helper
won’t emit events.
Instead, use onStateChange
or widgets to handle search behavior.A hook is called each time a search is requested
(with Algolia’s search helper
as a parameter).
Begin searching by calling helper.search()
.When modifying the state of the helper within search-function
,
the helper resets the page to 0.
To keep the current page, store the page information,
modify the state, and reapply pagination.JavaScript
Adds a
uiState
to your instantsearch
instance,
which provides an initial state to your widgets.Since this sets the initial state for your UI,
you need to include the corresponding widgets in your app.
For example, page: 5
as initial state only has an effect
if you include the pagination
widget as well.Replace YourIndexName
with the name of your Algolia index.JavaScript
Triggered when the state changes.
This can be helpful for performing custom logic on a state change.When using
onStateChange
, the instance is under your control.
You’re responsible for updating the UI state (with setUiState
).JavaScript
A time period (in ms) after which the search is considered to have stalled.
Read the slow network guide for more information.
JavaScript
The router configuration used to save the UI state into the URL or any client-side persistence.For more information, see Sync your URLs.You can’t use
initialUiState
with routing as the two options override each other.- Use
initialUiState
for simple and static use cases. - Use
routing
for anything complex or dynamic.
Enables the Insights middleware and loads the
search-insights
library (if not already loaded). The Insights middleware sends view and click events automatically, and lets you set up your own click and conversion events.To use this option with an object, refer to the Insights middleware options.Use
insights
instead.This function is exposed by the search-insights
library (usually window.aa
) and is required for sending click and conversion events with the insights
middleware.JavaScript
Test new InstantSearch features without affecting others.
Methods
Adds widgets to the
instantsearch
instance.You can add widgets to instantsearch
before and after you start it.JavaScript
Use
addWidgets([widget])
instead.Adds a widget to the instantsearch
instance.You can add widgets to instantsearch
before and after you start it.JavaScript
Finalizes the setup of
instantsearch
and triggers the first search.Call this method after you have added all your required widgets to instantsearch
.
You can also add widgets after instantsearch
has started,
but these widgets aren’t considered during searches made before you add them.JavaScript
removeWidgets
Removes widgets from the
instantsearch
instance.
You can only do this after you start instantsearch
.The widgets you remove from instantsearch
must implement a dispose()
method to reset the search parameters they manage.JavaScript
Use
removeWidgets([widget])
instead.Removes a widget from the instantsearch
instance.
You can only do this after you start instantsearch
.The widget you remove from instantsearch
must implement a dispose()
method to reset the search parameters they manage.JavaScript
dispose
Removes all widgets from the instance.
This method doesn’t trigger a search.
JavaScript
setUiState
refresh
Clears the Algolia responses cache and triggers a new search.
For more information, read the InstantSearch caching guide.
JavaScript
Properties
The status of the in-progress search.Possible values are:
idle
. No search is in progress.loading
. The search is loading. Useloading
for immediate feedback on an action. For loading indicators, use'stalled'
instead.stalled
. The search is stalled. This is triggered afterstalledSearchDelay
expires.error
. The search failed. The error is available in theerror
property.
JavaScript
The error that occurred during the search.
This is only defined when
status
is 'error'
.JavaScript
The
renderState
provides access to all the data to render the widgets,
including the methods to refine the search.
More information can be found in the renderState
page.JavaScript
Events
render
Triggered when all widgets are rendered.
This happens after every search request.
JavaScript
error
Triggered when calling the API reports an error.
JavaScript