Guides / Building Search UI

What is React InstantSearch?

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.

React InstantSearch is an open source UI library for React that lets you build a search interface in your frontend app.

InstantSearch focuses on enhancing your frontend with primitives that you can combine to create unique search interfaces.

InstantSearch supports server-side rendering and offers full routing capabilities.

InstantSearch suite includes various “flavors” to meet different development needs:

InstantSearch offers three levels of increasing control over your UI:

  1. Start with a predefined widget that you can configure and style with CSS.
  2. To change the render output of a widget, use Hooks to render what you want.
  3. To implement something that doesn’t exist, create a custom Hook.

Predefined widgets

The recommended way to use InstantSearch is with its predefined widgets such as <SearchBox>. InstantSearch includes a set of widgets that are most often used in search experiences. Widgets provide features and a rendered output. You can place them anywhere on your UI, configure them, and style them with CSS.

For example, add the <RefinementList>` widget and ask it to show a list of brands, so your users can refine their search using those brands.

1
2
3
4
5
6
7
8
9
10
11
12
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import { InstantSearch, RefinementList } from 'react-instantsearch';

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

function App() {
  return (
    <InstantSearch searchClient={searchClient} indexName="INDEX_NAME">
      <RefinementList attribute="brand" />
    </InstantSearch>
  );
}

The InstantSearch wrapper

The <InstantSearch> wrapper communicates between your app and Algolia. This is where you add all the widgets. It accepts a search client and an index name.

Refinement list widget

Refinement list widget

CSS theme

The predefined widgets in React InstantSearch are compatible with the default CSS theme:

Theme preview

Default theme preview

For more information, see Style your widgets.

If you use Hooks and you want to use the default theme in your app, follow the markup from the predefined widgets and use the InstantSearch class names.

Use Hooks

Algolia’s predefined widgets, with their fixed behavior and output, may not fully meet your requirements. For example, you might want to use React InstantSearch with a component library like Material UI or render to a non-DOM target like React Native.

To address these limitations, use Hooks to construct the UI you want:

For more information, see Customize a React InstantSearch widget.

Example component

Here’s an example of a custom <RefinementList> component created with useRefinementList(), and mounted in the <InstantSearch> provider.

1
2
3
4
5
6
7
8
9
10
11
12
13
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import { InstantSearch } from 'react-instantsearch';
import { RefinementList } from './RefinementList';

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

function App() {
  return (
    <InstantSearch searchClient={searchClient} indexName="instant_search">
      <RefinementList attribute="brand" />
    </InstantSearch>
  );
}

Pass stable references

When you provide a function to Hooks, make sure to pass a stable reference to avoid rendering endlessly (for example, with useCallback()). Objects and arrays are memoized; you don’t need to stabilize them.

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
26
27
28
29
import { liteClient as algoliasearch } from 'algoliasearch/lite';
import React, { useCallback } from 'react';
import { InstantSearch, useHits } from 'react-instantsearch';

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

function Search({ cartItems }) {
  const transformItems = useCallback(
    (items) =>
      items.map((item) => ({
        ...item,
        isInCart: Boolean(
          cartItems.find((cartItem) => cartItem.objectID === item.objectID)
        ),
      })),
    [cartItems]
  );
  const { hits } = useHits({ transformItems });

  return <>{/* Your JSX */}</>;
}

function App({ cartItems }) {
  return (
    <InstantSearch searchClient={searchClient} indexName="INDEX_NAME">
      <Search cartItems={cartItems} />
    </InstantSearch>
  );
}

Custom Hooks

React InstantSearch works with all InstantSearch.js connectors: from Algolia, from the community, or even your own.

To create a Hook:

  1. Create a connector.
  2. Use useConnector() to turn the connector into a Hook:
1
2
3
4
5
6
import { useConnector } from 'react-instantsearch';
import { connectMyWidget } from './connectMyWidget';

export function useMyWidget(props) {
  return useConnector(connectMyWidget, props);
}

You can now use your new Hook and component anywhere within <InstantSearch>.

Need help?

React InstantSearch is worked on full-time by Algolia’s JavaScript team.

Join the community

Ask questions and find answers on those following platforms.

Provide feedback

Stay up to date

Contributing?

All contributors are welcome, from casual to regular. Feel free to open a pull request.

Did you find this page helpful?