react-highlight-words vs react-highlighter vs react-syntax-highlighter
React Text Highlighting Libraries
react-highlight-wordsreact-highlighterreact-syntax-highlighterSimilar Packages:

React Text Highlighting Libraries

Text highlighting libraries in React are essential tools for enhancing user experience by allowing users to easily identify and focus on specific parts of text. These libraries offer various functionalities such as keyword highlighting, syntax highlighting for code snippets, and customizable styles, making them suitable for different use cases, from search functionality to code documentation. By integrating these libraries, developers can create more interactive and visually appealing applications that improve readability and engagement.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
react-highlight-words02,2992.42 MB7a year agoMIT
react-highlighter0160-248 years agoMIT
react-syntax-highlighter04,6442.19 MB13715 days agoMIT

Feature Comparison: react-highlight-words vs react-highlighter vs react-syntax-highlighter

Highlighting Methodology

  • react-highlight-words:

    react-highlight-words uses a dynamic approach to highlight words or phrases based on user input. It allows for real-time updates as the input changes, making it ideal for search functionalities where users can see their search terms highlighted immediately.

  • react-highlighter:

    react-highlighter provides a simpler method for highlighting text, focusing on static text without the need for complex configurations. It highlights text based on predefined keywords, making it straightforward for basic highlighting needs.

  • react-syntax-highlighter:

    react-syntax-highlighter specializes in syntax highlighting for code snippets. It parses the code and applies styles based on the language, ensuring that keywords, strings, and other syntax elements are visually distinct.

Customization

  • react-highlight-words:

    react-highlight-words offers extensive customization options, allowing developers to define styles for highlighted text, including colors, backgrounds, and font styles. This flexibility enables a seamless integration with existing UI designs.

  • react-highlighter:

    react-highlighter provides basic customization capabilities, focusing primarily on the color of the highlighted text. While it may not offer as many options as others, it is sufficient for simple use cases.

  • react-syntax-highlighter:

    react-syntax-highlighter comes with multiple themes and customization options, allowing developers to choose from a variety of styles for syntax highlighting. This makes it versatile for different applications and user preferences.

Performance

  • react-highlight-words:

    react-highlight-words is optimized for performance with efficient re-rendering strategies, ensuring that only the parts of the text that need highlighting are updated. This is crucial for applications with large text blocks or frequent updates.

  • react-highlighter:

    react-highlighter is lightweight and performs well for small to medium-sized text blocks. However, it may not be as efficient for larger datasets due to its simpler implementation.

  • react-syntax-highlighter:

    react-syntax-highlighter is designed for performance when rendering code snippets. It efficiently handles large blocks of code and minimizes re-renders, making it suitable for technical documentation.

Ease of Use

  • react-highlight-words:

    react-highlight-words is user-friendly and easy to implement, with clear documentation and examples. It is suitable for developers looking for a straightforward solution to highlight text dynamically.

  • react-highlighter:

    react-highlighter is very easy to use, requiring minimal setup. Its simplicity makes it a great choice for beginners or projects with basic highlighting needs.

  • react-syntax-highlighter:

    react-syntax-highlighter is also easy to use, especially for developers familiar with code syntax. It provides clear documentation and examples, making it accessible for those looking to integrate code highlighting.

Use Cases

  • react-highlight-words:

    react-highlight-words is best suited for applications that require dynamic text highlighting, such as search features, chat applications, or any context where user input needs to be highlighted in real-time.

  • react-highlighter:

    react-highlighter is ideal for simple text highlighting scenarios, such as highlighting keywords in articles or documents where user interaction is limited.

  • react-syntax-highlighter:

    react-syntax-highlighter is specifically designed for displaying code snippets in blogs, documentation, or educational platforms where syntax clarity is essential.

How to Choose: react-highlight-words vs react-highlighter vs react-syntax-highlighter

  • react-highlight-words:

    Choose react-highlight-words if you need to highlight specific words or phrases within a block of text dynamically. It is particularly useful for search functionalities where users can see their search terms highlighted in context.

  • react-highlighter:

    Select react-highlighter for a more straightforward approach to highlighting text. It is ideal for simple use cases where you want to highlight text based on user input without complex configurations or additional features.

  • react-syntax-highlighter:

    Opt for react-syntax-highlighter if your application involves displaying code snippets that need syntax highlighting. It supports multiple programming languages and offers various themes, making it suitable for technical documentation or code sharing.

README for react-highlight-words

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.

Try this example in Code Sandbox.

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={["and", "or", "the"]}
    autoEscape={true}
    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:

screen shot 2015-12-19 at 8 23 43 am

Props

PropertyTypeRequired?Description
activeClassNameStringThe class name to be applied to an active match. Use along with activeIndex
activeIndexNumberSpecify the match index that should be actively highlighted. Use along with activeClassName
activeStyleObjectThe inline style to be applied to an active match. Use along with activeIndex
autoEscapeBooleanEscape characters in searchWords which are meaningful in regular expressions
classNameStringCSS class name applied to the outer/wrapper <span>
caseSensitiveBooleanSearch should be case sensitive; defaults to false
findChunksFunctionUse a custom function to search for matching chunks. This makes it possible to use arbitrary logic when looking for matches. See the default findChunks function in highlight-words-core for signature. Have a look at the custom findChunks example on how to use it.
highlightClassNameString or ObjectCSS class name applied to highlighted text or object mapping search term matches to class names.
highlightStyleObjectInline styles applied to highlighted text
highlightTagNode or StringType of tag to wrap around highlighted matches. Defaults to mark but can also be a React component (class or functional)
sanitizeFunctionProcess each search word and text to highlight before comparing (eg remove accents); signature (text: string): string
searchWordsArray<String | RegExp>Array of search words. String search terms are automatically cast to RegExps unless autoEscape is true.
textToHighlightStringText to highlight matches in
unhighlightClassNameStringCSS class name applied to unhighlighted text
unhighlightStyleObjectInline styles applied to unhighlighted text
unhighlightTagNode or StringType of tag applied to unhighlighted parts. Defaults to span but can also be a React component (class or functional)
*anyAny other props (such as title or data-*) are applied to the outer/wrapper <span>

Custom highlight tag

By default, this component uses an HTML Mark Text element (<mark>) to wrap matched text, but you can inject a custom tag using the highlightTag property. This tag should be a React component that accepts the following properties:

PropertyTypeDescription
childrenStringText to be highlighted
highlightIndexNumberIndex of matched text

For example:

const Highlight = ({ children, highlightIndex }) => (
  <strong className="highlighted-text">{children}</strong>
);

Installation

yarn add react-highlight-words
npm i react-highlight-words

License

MIT License - fork, modify and use however you want.