UI libraries / React InstantSearch / Widgets

This is the React InstantSearch v7 documentation. React InstantSearch v7 is the latest version of React InstantSearch and the stable version of React InstantSearch Hooks.

If you were using React InstantSearch v6, you can upgrade to v7.

If you were using React InstantSearch Hooks, you can still use the React InstantSearch v7 documentation, but you should check the upgrade guide for necessary changes.

If you want to keep using React InstantSearch v6, you can find the archived documentation.

Signature
<InstantSearch
  indexName={string}
  searchClient={object}
  // Optional props
  initialUiState={object}
  onStateChange={function}
  stalledSearchDelay={number}
  routing={boolean | object}
  insights={boolean | object}
  future={{
    preserveSharedStateOnUnmount: boolean,
    persistHierarchicalRootCount: boolean,
  }}
/>
Import
1
import { InstantSearch } from 'react-instantsearch';

About this widget

<InstantSearch> is the root provider component of all widgets and Hooks.

You need two parameters:

The getting started guide will help you get up and running with React InstantSearch.

Middleware

React InstantSearch provides middleware to help you connect to other systems:

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import React from 'react';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch } from 'react-instantsearch';

const searchClient = algoliasearch(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);

function App() {
  return (
    <InstantSearch indexName="instant_search" searchClient={searchClient}>
      {/* Widgets */}
    </InstantSearch>
  );
}

Props

Parameter Description
indexName
type: string
Required

The main index in which to search.

1
2
3
4
5
6
<InstantSearch
  // ...
  indexName="instant_search"
>
  {/* Widgets */}
</InstantSearch>
searchClient
type: object
Required

Provides a search client to <InstantSearch>. Read the custom backend guidance on implementing a custom search client.

The client uses a cache to avoid unnecessary search operations, so you should use a stable reference to the same search client instance rather than creating a new one on each render. Avoid inlining the function call to algoliasearch as the prop value, and consider instantiating the client outside your React components.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const searchClient = algoliasearch(
  'YourApplicationID',
  'YourSearchOnlyAPIKey'
);

function App() {
  return (
    <InstantSearch
      // ...
      searchClient={searchClient}
    >
      {/* Widgets */}
    </InstantSearch>
  );
}
initialUiState
type: object

Provides an initial state to your React InstantSearch widgets using the uiState object from InstantSearch.js.

Replace YourIndexName with the name of your Algolia index.

1
2
3
4
5
6
7
8
9
10
11
<InstantSearch
  // ...
  initialUiState={{
    YourIndexName: {
      query: 'phone',
      page: 5,
    },
  }}
>
  {/* Widgets */}
</InstantSearch>
onStateChange
type: function

Triggers when the state changes. This can be useful when 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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const onStateChange = ({ uiState, setUiState }) => {
  // Custom logic
  setUiState(uiState);
};

function App() {
  return (
    <InstantSearch
      // ...
      onStateChange={onStateChange}
    >
      {/* Widgets */}
    </InstantSearch>
  );
}
stalledSearchDelay
type: number
default: 200

A time period (in ms) after which the search is considered to have stalled. Read the slow network guide for more information.

1
2
3
4
5
6
<InstantSearch
  // ...
  stalledSearchDelay={500}
>
  {/* Widgets */}
</InstantSearch>
routing
type: boolean | object

The router configuration used to save the UI state into the URL or any client-side persistence. The object is accepted if it has either of these keys:

  • router: object: this object is the part that saves the UI state. By default, it uses an instance of the history with the default parameters. It accepts:
    • onUpdate: function: adds an event listener that makes InstantSearch aware of external changes to the storage medium (such as the URL). Typically you’ll set up a listener for popstate, which triggers a callback with the current routeState.
    • read: function: reads the routing storage and gets routeState. It doesn’t take any parameters but returns an object.
    • write: function: pushes routeState into routing storage.
    • createURL: function: transforms routeState into a URL. It receives an object and returns a string (which may be empty).
    • dispose: function: cleans up all event listeners.
  • stateMapping: object: transforms the uiState into the object saved by the router. The default value is provided by simple. It accepts:
    • stateToRoute: function: transforms a ui-state representation into routeState. It receives an object that contains the UI state of all the widgets on the page. It can return any object that is readable by routeToState.
    • routeToState: function: transforms routeState into a ui-state representation. It receives an object that contains the UI state stored by the router. It can return any object that is readable by stateToRoute.

You can find more information in the routing guide.

1
2
3
4
5
6
<InstantSearch
  // ...
  routing={true}
>
  {/* Widgets */}
</InstantSearch>
insights
type: boolean | InsightsProps
default: false
Optional

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.

1
2
3
4
5
6
<InstantSearch
  // ...
  insights={true}
>
  {/* Widgets */}
</InstantSearch>
future
type: object
Optional

This option lets you try out new InstantSearch features without affecting the current experience for everyone else.

See below for more information on individual future options.

preserveSharedStateOnUnmount
since: v7.2.0
type: boolean
default: false
Optional

Changes the way dispose is used in InstantSearch lifecycle.

If false (the default), each widget unmounting will also remove its state, even if multiple widgets read that UI state.

If true, each widget unmounting will only remove its state if it’s the last of its type. This allows you to dynamically add and remove widgets without losing their state.

1
2
3
4
5
6
7
8
<InstantSearch
  // ...
  future={{
    preserveSharedStateOnUnmount: true,
  }}
>
  {/* Widgets */}
</InstantSearch>
persistHierarchicalRootCount
since: v7.4.1
type: boolean
default: false
Optional

Whether to display a constant facet value count at the root of a hierarchical menu with active refinements.

If false (default), the facet value count at the root level shows the facet value count of the refined (child) facet’s parent.

If true, the facet value count at the root level shows the sum of the facet value counts of all its children, with or without refined children.

1
2
3
4
5
6
7
8
<InstantSearch
  // ...
  future={{
    persistHierarchicalRootCount: true,
  }}
>
  {/* Widgets */}
</InstantSearch>
Did you find this page helpful?