React Syntax Highlighter

0.15.0 · maintenance · verified Sun Apr 19

react-highlight is a React component designed to provide syntax highlighting for code snippets within web applications by integrating the `highlight.js` library. It allows developers to display formatted code by passing code strings as children to the `Highlight` component or by rendering pre-processed HTML containing multiple code blocks. The current stable version available on npm is `0.15.0`. However, the project's README specifies `0.11.1` as the 'latest version', which suggests potential documentation lag or an irregular release cadence. A key differentiator is its direct, minimalist wrapper around `highlight.js`, making it straightforward for both single-snippet and multi-snippet highlighting, especially useful when parsing markdown-generated content. Styling is handled externally via `highlight.js` CSS themes, requiring manual inclusion.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `react-highlight` for a single JavaScript snippet and multiple snippets from an HTML string, including importing a `highlight.js` theme.

import React from 'react';
import Highlight from 'react-highlight';
import 'highlight.js/styles/github.css'; // Or any other highlight.js theme

function MyHighlightedCode() {
  const jsCode = `
const greet = (name) => {
  console.log('Hello, ' + name + '!');
};

greet('World');
`;

  const htmlContent = `
<h1>Example</h1>
<pre><code class="language-html">
  &lt;div&gt;Hello HTML&lt;/div&gt;
</code></pre>
<pre><code class="language-css">
  .container {
    color: blue;
  }
</code></pre>
`;

  return (
    <div>
      <h2>JavaScript Example</h2>
      <Highlight className='language-javascript'>
        {jsCode}
      </Highlight>

      <h2>Multi-snippet HTML Example</h2>
      <Highlight innerHTML={true}>
        {htmlContent}
      </Highlight>
    </div>
  );
}

export default MyHighlightedCode;

view raw JSON →