The <Highlight> component displays highlighted parts of a search result attribute.
Import
import { Highlight } from 'react-instantsearch';
Props
The search result object containing highlight information.<Highlight hit={hit} attribute="name" />
attribute
string | string[]
required
The attribute to highlight. Can be a string or array for nested attributes.// Simple attribute
<Highlight hit={hit} attribute="name" />
// Nested attribute
<Highlight hit={hit} attribute={['author', 'name']} />
The HTML tag to use for highlighted parts.<Highlight hit={hit} attribute="name" highlightedTagName="em" />
The HTML tag to use for non-highlighted parts.<Highlight
hit={hit}
attribute="name"
nonHighlightedTagName="span"
/>
The separator to use when the attribute is an array.<Highlight hit={hit} attribute="tags" separator=" | " />
CSS classes to customize the component styling.
Example
import { InstantSearch, SearchBox, Hits, Highlight } from 'react-instantsearch';
import { liteClient as algoliasearch } from 'algoliasearch/lite';
const searchClient = algoliasearch(
'YourApplicationID',
'YourSearchOnlyAPIKey'
);
function Hit({ hit }) {
return (
<article>
<h1>
<Highlight hit={hit} attribute="name" />
</h1>
<p>
<Highlight hit={hit} attribute="description" />
</p>
</article>
);
}
function App() {
return (
<InstantSearch searchClient={searchClient} indexName="products">
<SearchBox />
<Hits hitComponent={Hit} />
</InstantSearch>
);
}
With Custom Styling
function Hit({ hit }) {
return (
<article>
<h1>
<Highlight
hit={hit}
attribute="name"
highlightedTagName="strong"
classNames={{
highlighted: 'text-primary font-bold',
}}
/>
</h1>
</article>
);
}