> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# What is React InstantSearch?

> React InstantSearch is an open source library for building search interfaces.

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

export const AcademyLink = ({href, title}) => {
  return <Card horizontal title="Algolia Academy" href={href} icon="square-play">Learn more about: {title}</Card>;
};

export const FlavorSwitcher = ({current, baseHref = "", options = [], label = "InstantSearch framework"}) => {
  if (options.length === 0) {
    return <div className="not-prose" role="alert" style={{
      margin: "0.25rem 0 1.5rem",
      padding: "0.75rem",
      border: "1px solid #f59e0b",
      borderRadius: "0.625rem",
      color: "inherit",
      fontSize: "0.875rem"
    }}>
        FlavorSwitcher requires at least one option.
      </div>;
  }
  const selected = options.find(option => option.value === current) ?? options[0];
  return <div className="not-prose mint-flavor-switcher">
      <style>{`
        .mint-flavor-switcher {
          --mfs-bg: #ffffff;
          --mfs-bg-hover: #f4f4f5;
          --mfs-bg-current: #eef2ff;
          --mfs-border: #d4d4d8;
          --mfs-fg: #18181b;
          --mfs-muted: #71717a;
          --mfs-accent: #4f46e5;
          position: relative;
          width: min(100%, 19rem);
          margin: 0.25rem 0 1.5rem;
          color: var(--mfs-fg);
          font-size: 0.875rem;
          line-height: 1.25rem;
        }

        .dark .mint-flavor-switcher {
          --mfs-bg: #18181b;
          --mfs-bg-hover: #27272a;
          --mfs-bg-current: #272747;
          --mfs-border: #3f3f46;
          --mfs-fg: #fafafa;
          --mfs-muted: #a1a1aa;
          --mfs-accent: #a5b4fc;
        }

        .mint-flavor-switcher details {
          position: relative;
        }

        .mint-flavor-switcher summary {
          display: flex;
          min-height: 2.75rem;
          box-sizing: border-box;
          align-items: center;
          justify-content: space-between;
          gap: 0.75rem;
          padding: 0.625rem 0.75rem;
          border: 1px solid var(--mfs-border);
          border-radius: 0.625rem;
          background: var(--mfs-bg);
          color: var(--mfs-fg);
          cursor: pointer;
          font-weight: 600;
          list-style: none;
          transition: border-color 150ms ease, box-shadow 150ms ease;
        }

        .mint-flavor-switcher summary::-webkit-details-marker {
          display: none;
        }

        .mint-flavor-switcher summary:hover {
          border-color: var(--mfs-accent);
        }

        .mint-flavor-switcher summary:focus-visible {
          outline: 2px solid var(--mfs-accent);
          outline-offset: 2px;
        }

        .mint-flavor-switcher__label {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .mint-flavor-switcher__chevron {
          flex: none;
          transition: transform 150ms ease;
        }

        .mint-flavor-switcher details[open] .mint-flavor-switcher__chevron {
          transform: rotate(180deg);
        }

        .mint-flavor-switcher__menu {
          position: absolute;
          z-index: 50;
          top: calc(100% + 0.375rem);
          left: 0;
          width: 100%;
          box-sizing: border-box;
          margin: 0;
          padding: 0.375rem;
          border: 1px solid var(--mfs-border);
          border-radius: 0.625rem;
          background: var(--mfs-bg);
          box-shadow: 0 12px 30px rgb(0 0 0 / 16%);
          list-style: none;
        }

        .mint-flavor-switcher__menu li {
          margin: 0;
          padding: 0;
        }

        .mint-flavor-switcher__option {
          display: grid;
          gap: 0.125rem;
          padding: 0.625rem 0.75rem;
          border-radius: 0.4rem;
          color: var(--mfs-fg);
          text-decoration: none;
        }

        .mint-flavor-switcher__option:hover {
          background: var(--mfs-bg-hover);
        }

        .mint-flavor-switcher__option:focus-visible {
          outline: 2px solid var(--mfs-accent);
          outline-offset: -2px;
        }

        .mint-flavor-switcher__option[aria-current="page"] {
          background: var(--mfs-bg-current);
          color: var(--mfs-accent);
        }

        .mint-flavor-switcher__name {
          font-weight: 600;
        }

        .mint-flavor-switcher__description {
          color: var(--mfs-muted);
          font-size: 0.8125rem;
        }

        @media (prefers-reduced-motion: reduce) {
          .mint-flavor-switcher summary,
          .mint-flavor-switcher__chevron {
            transition: none;
          }
        }
      `}</style>

      <details>
        <summary aria-label={`${label}: ${selected.label}`}>
          <span className="mint-flavor-switcher__label">{selected.label}</span>
          <svg className="mint-flavor-switcher__chevron" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
            <path d="m6 9 6 6 6-6" />
          </svg>
        </summary>

        <ul className="mint-flavor-switcher__menu" aria-label={label}>
          {options.map(option => {
    const isCurrent = option.value === selected.value;
    const href = option.href ?? `${baseHref.replace(/\/$/, "")}/${encodeURIComponent(option.value)}`;
    return <li key={option.value}>
                <a className="mint-flavor-switcher__option" href={href} aria-current={isCurrent ? "page" : undefined}>
                  <span className="mint-flavor-switcher__name">
                    {option.label}
                  </span>
                  {option.description ? <span className="mint-flavor-switcher__description">
                      {option.description}
                    </span> : null}
                </a>
              </li>;
  })}
        </ul>
      </details>
    </div>;
};

<div className="mint-flavor-switcher-slot not-prose">
  <FlavorSwitcher
    current="react"
    baseHref="/doc/guides/building-search-ui/what-is-instantsearch"
    options={[
{ value: "js", label: "JavaScript", description: "InstantSearch.js" },
{ value: "react", label: "React", description: "React InstantSearch" },
{ value: "vue", label: "Vue", description: "Vue InstantSearch" },
{ value: "ios", label: "iOS", description: "InstantSearch iOS" },
{ value: "android", label: "Android", description: "InstantSearch Android" },
{ value: "flutter", label: "Flutter", description: "Algolia for Flutter" },
]}
  />
</div>

<Note>
  This is the **React InstantSearch v7** documentation.
  If you're upgrading from v6, see the [upgrade guide](/doc/guides/building-search-ui/upgrade-guides/react/#migrate-from-react-instantsearch-v6-to-react-instantsearch-v7).
  If you were using React InstantSearch Hooks,
  this v7 documentation applies—just check for [necessary changes](/doc/guides/building-search-ui/upgrade-guides/react/#migrate-from-react-instantsearch-hooks-to-react-instantsearch-v7).
  To continue using v6, you can find the [archived documentation](https://algolia.com/old-docs/deprecated/instantsearch/react/v6/api-reference/instantsearch/).
</Note>

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

InstantSearch supports [server-side rendering](/doc/guides/building-search-ui/going-further/server-side-rendering/react) and offers [full routing capabilities](/doc/guides/building-search-ui/going-further/routing-urls/react).

<Card title="Open CodeSandbox" icon="box" href="https://codesandbox.io/s/github/algolia/instantsearch/tree/master/examples/react/default-theme">
  Run and edit the What is React InstantSearch? example in CodeSandbox.
</Card>

<Frame>
  <iframe width="644" height="363" src="https://www.youtube.com/embed/H7Al2O_wHNY?start=1657" title="Integrate a search experience into a Next.js storefront with Algolia - Sarah Dayan" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen className="video-embed mb-4" />
</Frame>

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`](/doc/api-reference/widgets/search-box/react).
InstantSearch includes a [set of widgets](/doc/guides/building-search-ui/widgets/showcase/react) 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`](/doc/api-reference/widgets/refinement-list/react)\` widget and ask it to show a list of brands, so your users can refine their search using those brands.

```jsx React icon=code theme={"system"}
import { liteClient as algoliasearch } from "algoliasearch/lite";
import { InstantSearch, RefinementList } from "react-instantsearch";

const appID = "ALGOLIA_APPLICATION_ID";
const apiKey = "ALGOLIA_SEARCH_API_KEY";
const indexName = "INDEX_NAME";

const searchClient = algoliasearch(appID, apiKey);

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

### The InstantSearch wrapper

The [`InstantSearch`](/doc/api-reference/widgets/instantsearch/react) wrapper communicates between your app and Algolia.
This is where you add all the widgets.
It accepts a search client and an <Index /> name.

<img src="https://mintcdn.com/algolia/WOi5v-PGZrMZ2rOj/images/build-search-ui/refinement-list.png?fit=max&auto=format&n=WOi5v-PGZrMZ2rOj&q=85&s=924b80863d38a2d12abded6187d2cda4" alt="Screenshot of a refinement list widget showing brand filters with checkboxes and count labels." width="166" height="320" data-path="images/build-search-ui/refinement-list.png" />

Refinement list widget

### CSS theme

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

<img src="https://mintcdn.com/algolia/PPYT_t3uPKSP6jma/images/instantsearch/common/what-is-instantsearch/theme-preview.png?fit=max&auto=format&n=PPYT_t3uPKSP6jma&q=85&s=ab062f696477da133dcdf6a482afddf5" alt="Screenshot of a search results page with product cards, a refinement list, and pagination controls." width="930" height="560" data-path="images/instantsearch/common/what-is-instantsearch/theme-preview.png" />

Default theme preview

For more information, see [Style your widgets](/doc/guides/building-search-ui/widgets/customize-an-existing-widget/react#style-your-widgets).

<Tip>
  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.
</Tip>

## 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](https://mui.com/material-ui/) or render to a non-DOM target like [React Native](https://reactnative.dev/).

To address these limitations, use [Hooks](https://react.dev/reference/react/hooks) to construct the UI you want:

* [`useBreadcrumb`](/doc/api-reference/widgets/breadcrumb/react#hook)
* [`useClearRefinements`](/doc/api-reference/widgets/clear-refinements/react#hook)
* [`useConfigure`](/doc/api-reference/widgets/configure/react#hook)
* [`useCurrentRefinements`](/doc/api-reference/widgets/current-refinements/react#hook)
* [`useDynamicWidgets`](/doc/api-reference/widgets/dynamic-facets/react#hook)
* [`useHierarchicalMenu`](/doc/api-reference/widgets/hierarchical-menu/react#hook)
* [`useHits`](/doc/api-reference/widgets/hits/react#hook)
* [`useHitsPerPage`](/doc/api-reference/widgets/hits-per-page/react#hook)
* [`useInfiniteHits`](/doc/api-reference/widgets/infinite-hits/react#hook)
* [`useMenu`](/doc/api-reference/widgets/menu/react#hook)
* [`usePagination`](/doc/api-reference/widgets/pagination/react#hook)
* [`usePoweredBy`](/doc/api-reference/widgets/powered-by/react#hook)
* [`useRange`](/doc/api-reference/widgets/range-input/react#hook)
* [`useRefinementList`](/doc/api-reference/widgets/refinement-list/react#hook)
* [`useSearchBox`](/doc/api-reference/widgets/search-box/react#hook)
* [`useSortBy`](/doc/api-reference/widgets/sort-by/react#hook)
* [`useStats`](/doc/api-reference/widgets/stats/react#hook)
* [`useToggleRefinement`](/doc/api-reference/widgets/toggle-refinement/react#hook)

For more information, see [Customize a React InstantSearch widget](/doc/guides/building-search-ui/widgets/customize-an-existing-widget/react).

### Example component

Here's an example of a custom `RefinementList` component created with
[`useRefinementList`](/doc/api-reference/widgets/refinement-list/react#hook),
and mounted in the [`InstantSearch`](/doc/api-reference/widgets/instantsearch/react) provider.

<CodeGroup>
  ```jsx App.js theme={"system"}
  import { liteClient as algoliasearch } from "algoliasearch/lite";
  import { InstantSearch } from "react-instantsearch";
  import { RefinementList } from "./RefinementList";

  const searchClient = algoliasearch("undefined", "undefined");

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

  ```jsx RefinementList.js theme={"system"}
  import { useRefinementList } from "react-instantsearch";

  export function RefinementList(props) {
    // Retrieves the refinement `items` and the `refine` function to update the
    // refinement
    const { items, refine } = useRefinementList(props);

    return (
      <div className="ais-RefinementList">
        <ul className="ais-RefinementList-list">
          {items.map((item) => (
            <li
              key={item.value}
              className={cx(
                "ais-RefinementList-item",
                item.isRefined && "ais-RefinementList-item--selected",
              )}
            >
              <label className="ais-RefinementList-label">
                <input
                  className="ais-RefinementList-checkbox"
                  type="checkbox"
                  value={item.value}
                  checked={item.isRefined}
                  onChange={() => refine(item.value)}
                />
                <span className="ais-RefinementList-labelText">{item.label}</span>
                <span className="ais-RefinementList-count">{item.count}</span>
              </label>
            </li>
          ))}
        </ul>
      </div>
    );
  }

  function cx(...classNames) {
    return classNames.filter(Boolean).join(" ");
  }
  ```
</CodeGroup>

### 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`](https://reactjs.org/docs/hooks-reference.html#usecallback)).
You don't need to stabilize objects and arrays, because React reuses them between renders unless you create new ones.

```jsx React icon=code theme={"system"}
import { liteClient as algoliasearch } from "algoliasearch/lite";
import React, { useCallback } from "react";
import { InstantSearch, useHits } from "react-instantsearch";

const appID = "ALGOLIA_APPLICATION_ID";
const apiKey = "ALGOLIA_SEARCH_API_KEY";

const searchClient = algoliasearch(appID, apiKey);

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](/doc/guides/building-search-ui/widgets/create-your-own-widgets/react#build-a-custom-connector).
2. Use [`useConnector`](/doc/api-reference/widgets/connector/react) to turn the connector into a Hook:

<CodeGroup>
  ```js useMyWidget.js theme={"system"}
  import { useConnector } from "react-instantsearch";
  import { connectMyWidget } from "./connectMyWidget";

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

  ```jsx MyComponent.js theme={"system"}
  import { useMyWidget } from "./useMyWidget";

  export function MyComponent(props) {
    const data = useMyWidget(props);

    // Render based on the data returned by the Hook
    return null;
  }
  ```

  ```jsx App.js theme={"system"}
  import { liteClient as algoliasearch } from "algoliasearch/lite";
  import { InstantSearch } from "react-instantsearch";
  import { MyComponent } from "./MyComponent";

  const searchClient = algoliasearch("undefined", "undefined");

  function App() {
    return (
      <InstantSearch searchClient={searchClient} indexName="instant_search">
        <MyComponent />
      </InstantSearch>
    );
  }
  ```
</CodeGroup>

You can now use your new Hook and component anywhere within [`InstantSearch`](/doc/api-reference/widgets/instantsearch/react).

## Resources

Use the following links and resources to learn more.

### See also

* [Install React InstantSearch](/doc/guides/building-search-ui/installation/react)
* [Get started](/doc/guides/building-search-ui/getting-started/react)
* [API reference](/doc/api-reference/widgets/react)
* [Code Exchange](https://www.algolia.com/developers/code-exchange/?category=frontend-tools)
* [Support knowledge base](https://support.algolia.com/hc/en-us/sections/19109461315345-InstantSearch-React-InstantSearch-V7)

### Join the community

Ask questions and find answers on those following platforms.

* [Algolia Community Discord](https://alg.li/discord)
* [Stack Overflow](https://stackoverflow.com/questions/tagged/instantsearch)
* [GitHub issues](https://github.com/algolia/instantsearch/issues)
* [Changelog](https://github.com/algolia/instantsearch-android/blob/master/CHANGELOG.md).
