What is react-highlight-words?
The react-highlight-words package is a React component used to highlight words within a larger body of text. It is useful for search results, text highlighting, and other applications where specific words or phrases need to be visually emphasized.
What are react-highlight-words's main functionalities?
Basic Highlighting
This feature allows you to highlight specific words within a text. The `searchWords` array contains the words to be highlighted, and the `textToHighlight` is the text where the words will be highlighted.
import React from 'react';
import Highlighter from 'react-highlight-words';
const text = 'This is a sample text to demonstrate highlighting.';
const searchWords = ['sample', 'highlighting'];
const App = () => (
<Highlighter
highlightClassName="YourHighlightClass"
searchWords={searchWords}
autoEscape={true}
textToHighlight={text}
/>
);
export default App;
Custom Highlighting Styles
This feature allows you to apply custom styles to the highlighted words. The `highlightStyle` prop is used to define the CSS styles for the highlighted text.
import React from 'react';
import Highlighter from 'react-highlight-words';
const text = 'This is a sample text to demonstrate custom highlighting.';
const searchWords = ['sample', 'custom'];
const App = () => (
<Highlighter
highlightStyle={{ backgroundColor: 'yellow', fontWeight: 'bold' }}
searchWords={searchWords}
autoEscape={true}
textToHighlight={text}
/>
);
export default App;
Case Insensitive Highlighting
This feature allows you to perform case insensitive highlighting. By setting the `caseSensitive` prop to `false`, the component will highlight words regardless of their case.
import React from 'react';
import Highlighter from 'react-highlight-words';
const text = 'This is a Sample text to demonstrate case insensitive highlighting.';
const searchWords = ['sample', 'highlighting'];
const App = () => (
<Highlighter
highlightClassName="YourHighlightClass"
searchWords={searchWords}
autoEscape={true}
caseSensitive={false}
textToHighlight={text}
/>
);
export default App;
Other packages similar to react-highlight-words
react-text-highlighter
The react-text-highlighter package provides similar functionality for highlighting text within a React component. It offers customizable styles and supports case insensitive highlighting. Compared to react-highlight-words, it has a simpler API but fewer customization options.
react-string-replace
The react-string-replace package allows you to replace parts of a string with React components. It can be used to highlight text by replacing specific words with styled components. While it is more flexible in terms of what can be replaced, it requires more manual setup compared to react-highlight-words.
react-markdown
The react-markdown package is used to render Markdown content in React. It supports custom renderers, which can be used to highlight specific words or phrases. Although it is primarily designed for Markdown rendering, it can be adapted for text highlighting with custom renderers.
React component to highlight words within a larger body of text.
Check out a demo here.
Usage
To use it, just provide it with an array of search terms and a body of text to highlight:
<Highlighter
highlightClassName='YourHighlightClass'
searchWords={['and', 'or', 'the']}
textToHighlight="The dog is chasing the cat. Or perhaps they're just playing?"
/>
And the Highlighter
will mark all occurrences of search terms within the text:
Props
Property | Type | Required? | Description |
---|
highlightClassName | String | | CSS class name applied to highlighted text |
highlightStyle | Object | | Inline styles applied to highlighted text |
sanitize | Function | | Process each search word and text to highlight before comparing (eg remove accents); signature (text: string): string |
searchWords | Array | ✓ | Array of search words |
textToHighlight | String | ✓ | Text to highlight matches in |
Installation
npm i --save react-highlight-words
License
MIT License - fork, modify and use however you want.