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

# Snippet

> Shortens attributes from hits for display in search results.

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

```tsx Signature theme={"system"}
<Snippet
  attribute={string}
  hit={object}
  // Optional props
  highlightedTagName={ReactType}
  nonHighlightedTagName={ReactType}
  separator={ReactNode}
  classNames={object}
  ...props={ComponentProps<'span'>}
/>
```

## Import

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

<Card title="See this widget in action" icon="monitor-play" href="https://instantsearchjs.netlify.app/stories/react/?path=/story/snippet--default" horizontal>
  Preview this widget and its behavior.
</Card>

## About this widget

`<Snippet>` is a widget that displays attributes in a shorter form (a snippet).
Snippeted attributes are also [highlighted](/doc/api-reference/widgets/highlight/react).

It uses Algolia's [snippeting feature](/doc/guides/building-search-ui/ui-and-ux-patterns/highlighting-snippeting/react#snippeting)
with the `hit` object provided by [`<Hits>`](/doc/api-reference/widgets/hits/react),
[`<InfiniteHits>`](/doc/api-reference/widgets/infinite-hits/react), or their Hooks.

To determine which [attributes](#param-attribute) should be snippeted,
set them from [the Algolia dashboard](https://dashboard.algolia.com/explorer/configuration/snippeting),
[the CLI](/doc/tools/cli/commands/objects/browse#highlighting-and-snippeting),
or with the API (using the [`attributesToSnippet`](/doc/api-reference/api-parameters/attributesToSnippet) parameter):

```jsx JavaScript icon=code theme={"system"}
<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:

```json JSON icon=braces theme={"system"}
{
  "_snippetResult": {
    "attributeName": {
      "value": "..."
    }
  }
}
```

## Examples

```jsx JavaScript icon=code theme={"system"}
import React from "react";
import { liteClient as 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

<ParamField body="attribute" type="keyof THit" required>
  The attribute to snippet in the record.

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

  ```jsx JavaScript icon=code theme={"system"}
  <Snippet
    // ...
    attribute="description"
  />;
  ```
</ParamField>

<ParamField body="hit" type="THit" required>
  The original `hit` object provided to the component.

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

  ```jsx JavaScript icon=code theme={"system"}
  <Snippet
    // ...
    hit={hit}
  />;
  ```
</ParamField>

<ParamField body="highlightedTagName" type="React.ReactType<any>" default="mark">
  The name of the HTML element to wrap the highlighted parts of the string with.

  ```jsx JavaScript icon=code theme={"system"}
  <Snippet
    // ...
    highlightedTagName="em"
  />;
  ```
</ParamField>

<ParamField body="nonHighlightedTagName" type="React.ReactType<any>" default="span">
  The name of the HTML element to wrap the non-highlighted parts of the string with.

  ```jsx JavaScript icon=code theme={"system"}
  <Snippet
    // ...
    nonHighlightedTagName="div"
  />;
  ```
</ParamField>

<ParamField body="separator" type="React.ReactNode" default=",">
  The character between each item when the attribute to highlight is an array.

  ```jsx JavaScript icon=code theme={"system"}
  <Snippet
    // ...
    separator=" - "
  />;
  ```
</ParamField>

<ParamField body="classNames" type="Partial<SnippetClassNames>">
  The [CSS classes you can override](/doc/guides/building-search-ui/widgets/customize-an-existing-widget/react#style-your-widgets) and pass to the widget's elements.
  It's useful to style widgets with class-based CSS frameworks like [Bootstrap](https://getbootstrap.com) or [Tailwind CSS](https://tailwindcss.com).

  * `root`. The root element of the widget.
  * `highlighted`. The highlighted parts.
  * `nonHighlighted`. The non-highlighted parts.
  * `separator`. The separator elements between highlighted parts.

  ```jsx JavaScript icon=code theme={"system"}
  <Snippet
    // ...
    classNames={{
      root: "MyCustomSnippet",
      separator: "MyCustomSnippetSeparator MyCustomSnippetSeparator--subclass",
    }}
  />;
  ```
</ParamField>

<ParamField body="...props" type="React.ComponentProps<'span'>">
  Any `<span>` prop to forward to the root element of the widget.

  ```jsx JavaScript icon=code theme={"system"}
  <Snippet
    // ...
    className="MyCustomSnippet"
    title="My custom title"
  />;
  ```
</ParamField>
