Does <Highlight> support arrays?

I’m coming from React InstantSearch: Highlight on field with array value and I haven’t seen any updates since 2017 so I don’t know what is the current status for this. Does Algolia component support array values?

Let’s say I have an array of tags: tags: ["tag1", "tag2"]

Inside my loop, I want to display and highlight those elements in:, index) => <li className={`article__tag__item`} key={tag}>
          <Highlight attribute={`tags`} hit={hit} tagName="mark"/>

However, it throws an error:

index.js:2177 TypeError: Cannot read property ‘0’ of undefined
at Highlighter.js:50

I’ve found a workaround using:

  const highlightedAttr = hit._highlightResult.tags
    .map(attr => attr.value)
    .map(attr => attr.replace(/<ais-highlight-[0-9]+/, `<markclass="ais-Highlight__highlighted"`).replace(/<\/ais-highlight-[0-9]+/, `</mark`)), index) => <li className={`article__tag__item`} key={tag}>
              <span dangerouslySetInnerHTML={{__html: highlightedAttr}} />


Despite working almost as expected, the search query does not support whitespaces.

I would like to know how to implement a <Highlight> if support arrays and if don’t, a suitable workaround.

With a bit more research I’ve found the solution. I hope it help people to avoid the work of diving between threads :rofl:. This does the trick:

<Highlight attribute={tags[${index}]} hit={hit} tagName={mark} />


@fbuireu Thanks for the React solution.

Would you know to write the same thing in vanilla JS?


With InstantSearch.js, you can leverage the templates property on the hits widget and template each item with the item method:

    container: '#hits',
    templates: {
      item(item) {
        return `<article>
          .map(({ value }) => `<li>${value}</li>`)


1 Like