React Highlight Words

0.21.0 · active · verified Tue Apr 21

react-highlight-words is a React component designed to highlight specific words or phrases within a larger body of text. It is currently stable at version v0.21.0, which includes support for React 19. The package primarily sees updates for compatibility with newer React versions and minor feature additions, indicating a consistent but not overly frequent release cadence. Key differentiators include its straightforward API, support for regular expression `searchWords`, automatic escaping of special characters via `autoEscape`, and the ability to apply specific styling or classes to an 'active' match using `activeIndex` and `activeClassName`, making it suitable for search result navigation or interactive text features. It provides a robust solution for visually emphasizing text segments based on user-defined criteria.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of the Highlighter component, highlighting multiple search words within a given text and applying a custom class to matches.

import React from 'react';
import { createRoot } from 'react-dom/client';
import Highlighter from 'react-highlight-words';

const root = createRoot(document.getElementById('root'));
root.render(
  <Highlighter
    highlightClassName="YourHighlightClass"
    searchWords={['dog', 'cat', 'playing']}
    autoEscape={true}
    textToHighlight="The dog is chasing the cat. Or perhaps they're just playing?"
  />
);

view raw JSON →