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

# Build a Query Suggestions UI

> Use the Autocomplete widget to show Query Suggestions and recent searches.

export const customLabel_0 = undefined

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>;

<div className="not-prose algolia-flavor-switcher">
  <div className="afs-dropdown">
    <div className="afs-trigger" role="button" tabIndex="0" aria-haspopup="listbox">
      <span className="afs-current">React</span>

      <svg className="afs-chevron lucide lucide-chevron-down" width="24" height="24" 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>
    </div>

    <ul className="afs-menu" role="listbox">
      <li role="option" aria-selected="false"><a className="afs-option" href="/doc/guides/building-search-ui/ui-and-ux-patterns/autocomplete/query-suggestions/js"><span className="afs-option-name">JavaScript</span><span className="afs-option-lib">InstantSearch.js</span></a></li>
      <li role="option" aria-selected="true"><a className="afs-option is-current" href="/doc/guides/building-search-ui/ui-and-ux-patterns/autocomplete/query-suggestions/react"><span className="afs-option-name">React</span><span className="afs-option-lib">React InstantSearch</span></a></li>
    </ul>
  </div>
</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>

Use [Query Suggestions](/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/react) to help users find popular searches as they type.
Query Suggestions creates an <Index /> from popular searches.
You can use this index to show search suggestions.

After you configure the Query Suggestions index, use the [`<Autocomplete>`](/doc/api-reference/widgets/autocomplete/react) widget to show suggestions.
When users select a suggestion, the widget navigates to the suggestion URL or refines the search query, depending on your configuration.

<Columns>
  <Card title="Open CodeSandbox" icon="codesandbox" href="https://codesandbox.io/s/github/algolia/instantsearch/tree/master/examples/react/query-suggestions">
    Run and edit the Query Suggestions UI example in CodeSandbox.
  </Card>

  <Card title="Explore source code" icon="github" href="https://github.com/algolia/instantsearch/tree/master/examples/react/query-suggestions">
    Browse the source code for the Query Suggestions UI example on GitHub.
  </Card>
</Columns>

<Callout icon="flask-conical" color="#14b8a6">
  This widget is **{customLabel_0 || "experimental"}** and is subject to change in minor versions.
</Callout>

## Add Query Suggestions and recent searches

The widget displays suggestions from a [Query Suggestions index](/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/react) and recent searches stored in the browser's local storage.
Enable them with the [`showQuerySuggestions`](/doc/api-reference/widgets/autocomplete/react/#param-showquerysuggestions) and [`showRecent`](/doc/api-reference/widgets/autocomplete/react/#param-showrecent) props:

```jsx React icon=code theme={"system"}
<EXPERIMENTAL_Autocomplete
  placeholder="Search for products"
  showRecent
  showQuerySuggestions={{
    indexName: "instant_search_demo_query_suggestions",
    getURL: (item) => `/search?q=${item.query}`,
  }}
  indices={[
    {
      indexName: "instant_search",
      // ...
    },
  ]}
/>
```

## Refine results with suggestions

To keep users on the same page, set up a multi-index search experience that uses the selected suggestion to search the main index.

The [`<Autocomplete>`](/doc/api-reference/widgets/autocomplete/react) widget targets the index that contains the suggestions.
The other widgets target the main index that contains your records.
For more information, see [Multi-index search](/doc/guides/building-search-ui/ui-and-ux-patterns/multi-index-search/react).

```jsx React icon=code expandable theme={"system"}
// ...

import { EXPERIMENTAL_Autocomplete } from 'react-instantsearch';

export function App() {
  return (
    <InstantSearch
      searchClient={searchClient}
      indexName="instant_search"
    >
      <EXPERIMENTAL_Autocomplete
        showQuerySuggestions={{
          indexName: 'instant_search_demo_query_suggestions',
        }}
      />
      <Hits hitComponent={HitComponent} />
    </InstantSearch>
  )
}
```

## Customize recent searches

Pass an object to `showRecent` to customize how each recent search renders:

```jsx React icon=code theme={"system"}
<EXPERIMENTAL_Autocomplete
  showRecent={{
    itemComponent: ({ item, onSelect }) => (
      <button type="button" onClick={onSelect}>
        {item.query}
      </button>
    ),
  }}
  // ...
/>
```

## Customize suggestions

Add an `itemComponent` to `showQuerySuggestions` to template each suggestion, and `searchParameters` to apply [search parameters](/doc/api-reference/search-api-parameters) to the suggestions query:

```jsx React icon=code theme={"system"}
import { ReverseHighlight } from "react-instantsearch";

<EXPERIMENTAL_Autocomplete
  showQuerySuggestions={{
    indexName: "instant_search_demo_query_suggestions",
    searchParameters: { hitsPerPage: 5 },
    itemComponent: ({ item, onSelect }) => (
      <button type="button" onClick={onSelect}>
        <ReverseHighlight hit={item} attribute="query" />
      </button>
    ),
  }}
  // ...
/>;
```

Use `<ReverseHighlight>` to emphasize the text that doesn't match the query.
This is a common style for Query Suggestions.

The widget has no built-in category grouping for suggestions and there's no `categoryAttribute`.
To show a category next to a suggestion, render the category in the suggestion's `item` template.

## See also

* [Autocomplete](/doc/guides/building-search-ui/ui-and-ux-patterns/autocomplete/react) guide for the widget basics.
