🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
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
<Snippet
  attribute={string}
  hit={object}
  // Optional props
  highlightedTagName={ReactType}
  nonHighlightedTagName={ReactType}
  separator={ReactNode}
  classNames={object}
  ...props={ComponentProps<'span'>}
/>
Import
1
import { Snippet } from 'react-instantsearch';

About this widget

<Snippet> is a widget that displays attributes in a shorter form (a snippet). Snippeted attributes are also highlighted.

It uses Algolia’s snippeting feature with the hit object provided by <Hits>, <InfiniteHits>, or their Hooks.

To determine which attributes should be snippeted, first set them from the Algolia dashboard, the CLI, or with the API (using the attributesToSnippet parameter):

1
<Configure attributesToSnippet={['description']} />

With attributesToSnippet, you can also set the snippet’s size to a specific number of words (it defaults to 10). For example, attributesToSnippet={['description:5']}.

You can even use this widget with objects other than Algolia search results. The only requirement is that the provided value must have the following structure:

1
2
3
4
5
6
7
{
  "_snippetResult": {
    "attributeName": {
      "value": "..."
    }
  }
}

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import React from 'react';
import algoliasearch from 'algoliasearch/lite';
import { InstantSearch, Hits, Snippet } from 'react-instantsearch';

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

function Hit({ hit }) {
  return (
    <article>
      <Snippet hit={hit} attribute="description" />
    </article>
  );
}

function App() {
  return (
    <InstantSearch indexName="instant_search" searchClient={searchClient}>
      <Hits hitComponent={Hit} />
    </InstantSearch>
  );
}

Props

Parameter Description
attribute
type: keyof THit
Required

The attribute to snippet in the record.

For deeply nested objects, specify a dot-separated value like actor.bio.

1
2
3
4
<Snippet
  // ...
  attribute="description"
/>
hit
type: THit
Required

The original hit object provided to the component.

The object must have a _snippetResult[attribute].value property.

1
2
3
4
<Snippet
  // ...
  hit={hit}
/>
highlightedTagName
type: React.ReactType<any>
default: "mark"
Optional

The name of the HTML element to wrap the highlighted parts of the string with.

1
2
3
4
<Snippet
  // ...
  highlightedTagName="em"
/>
nonHighlightedTagName
type: React.ReactType<any>
default: "span"
Optional

The name of the HTML element to wrap the non-highlighted parts of the string with.

1
2
3
4
<Snippet
  // ...
  nonHighlightedTagName="div"
/>
separator
type: React.ReactNode
default: ", "
Optional

The character between each item when the attribute to snippet is an array.

1
2
3
4
<Snippet
  // ...
  separator=" - "
/>
classNames
type: Partial<SnippetClassNames>
Optional

CSS classes to pass to the widget’s elements. This is useful to style widgets with class-based CSS frameworks like Bootstrap or Tailwind CSS.

  • root: The root element of the widget.
  • highlighted: The highlighted parts.
  • nonHighlighted: The non-highlighted parts.
  • separator: The separator elements between highlighted parts.
1
2
3
4
5
6
7
<Snippet
  // ...
  classNames={{
    root: 'MyCustomSnippet',
    separator: 'MyCustomSnippetSeparator MyCustomSnippetSeparator--subclass',
  }}
/>
...props
type: React.ComponentProps<'span'>
Optional

Any <span> prop to forward to the root element of the widget.

1
2
3
4
5
<Snippet
  // ...
  className="MyCustomSnippet"
  title="My custom title"
/>
Did you find this page helpful?