React Syntax Highlighter
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
-
SyntaxError: Named export 'Highlight' not found. Did you mean 'default'?
cause `Highlight` is a default export, but you are trying to import it as a named export.fixChange your import statement from `import { Highlight } from 'react-highlight'` to `import Highlight from 'react-highlight'`. -
Code snippets are displayed as plain text without any highlighting colors or formatting.
cause The required `highlight.js` CSS theme has not been imported or linked, meaning there are no styles applied to the highlighted code.fixAdd an import for a `highlight.js` theme, for example: `import 'highlight.js/styles/atom-one-dark.css';` in your main application entry file or component. -
Warning: Each child in a list should have a unique 'key' prop. (When using `innerHTML={true}` with React Developer Tools enabled).cause While `react-highlight` handles `innerHTML` internally, React often warns about dynamic content without keys if it expects a list of children. This warning typically arises when React's reconciler attempts to optimize or track elements within the `dangerouslySetInnerHTML` output, although `react-highlight` itself might not directly produce list children.fixThis warning is often benign in the context of `dangerouslySetInnerHTML` for a single HTML string with `react-highlight`, as the component manages the inner HTML. If it becomes problematic or indicates a deeper issue with how the HTML is structured, ensure the HTML content itself is well-formed.
Warnings
- breaking The README states 'Latest version `0.11.1`' while the npm package is `0.15.0`. This discrepancy can lead to confusion regarding API changes or available features between what's documented and what's published. Always refer to the `npm` changelog or GitHub releases for the most accurate information on `0.15.0`.
- gotcha Syntax highlighting styles are not bundled with `react-highlight`. You must explicitly import or link a `highlight.js` CSS theme (e.g., `import 'highlight.js/styles/github.css';`) for any styling to appear. Without this, code will not be visibly highlighted.
- gotcha Using `innerHTML={true}` to highlight multiple code snippets can expose your application to Cross-Site Scripting (XSS) vulnerabilities if the HTML content is not from a trusted source. Ensure that any HTML passed with `innerHTML` is sanitized.
Install
-
npm install react-highlight -
yarn add react-highlight -
pnpm add react-highlight
Imports
- Highlight
import { Highlight } from 'react-highlight'import Highlight from 'react-highlight'
- Highlight (CommonJS)
const Highlight = require('react-highlight');const Highlight = require('react-highlight').default; - highlight.js themes
import { github } from 'highlight.js/styles';import 'highlight.js/styles/github.css';
Quickstart
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">
<div>Hello HTML</div>
</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;