These libraries address two distinct needs in React applications: displaying highlighted code blocks and providing editable code environments. prism-react-renderer, react-highlight, and react-syntax-highlighter focus on static syntax highlighting for documentation or blogs, while react-codemirror provides a full code editor experience with input handling. Choosing the right tool depends on whether you need users to read code or write it, and how much control you need over the rendering process.
When adding code blocks to a React application, you are usually solving one of two problems: displaying static code snippets or providing an editable code environment. The packages prism-react-renderer, react-highlight, and react-syntax-highlighter handle static highlighting, while react-codemirror handles editing. Understanding the architectural differences between these tools helps you avoid performance pitfalls and maintenance debt.
The core difference lies in how much access you have to the highlighted tokens. Some libraries give you a ready-made component, while others give you the data to build your own.
prism-react-renderer provides a low-level render prop pattern. It parses the code and gives you an array of tokens to map over. This allows you to render each token as a custom React component instead of a plain <span>.
// prism-react-renderer: Custom token rendering
import { Prism } from 'prism-react-renderer';
<Prism code={code} language={language}>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={className} style={style}>
{tokens.map((line, i) => (
<div key={i} {...getLineProps({ line })}>
{line.map((token, key) => (
<span key={key} {...getTokenProps({ token })} />
))}
</div>
))}
</pre>
)}
</Prism>
react-syntax-highlighter abstracts the rendering logic into a single component. You pass the code as children and select a theme. It handles the token mapping internally.
// react-syntax-highlighter: High-level component
import { Prism } from 'react-syntax-highlighter';
<Prism language={language} theme={theme}>
{code}
</Prism>
react-highlight is the simplest wrapper. It applies a class name to a standard HTML tag and relies on highlight.js to manipulate the DOM internally.
// react-highlight: Minimal wrapper
import Highlight from 'react-highlight';
<Highlight className={language}>
{code}
</Highlight>
react-codemirror does not render static HTML. It mounts an editor instance that manages its own DOM structure for input handling and highlighting simultaneously.
// react-codemirror: Editor instance
import CodeMirror from 'react-codemirror';
<CodeMirror
value={code}
options={{ mode: language, lineNumbers: true }}
onChange={newCode => setCode(newCode)}
/>
Support for languages and themes varies based on the underlying engine (Prism.js vs. highlight.js) and how the package loads them.
prism-react-renderer relies on Prism.js. You must manually import the languages you need to keep bundles small. Themes are handled via CSS or styled-components.
// prism-react-renderer: Manual language import
import Prism from 'prism-react-renderer/prism';
(typeof global !== 'undefined' ? global : window).Prism = Prism;
require('prismjs/components/prism-jsx');
// Theme applied via className or style prop
<Prism theme={myCustomTheme} ... />
react-syntax-highlighter supports both Prism and highlight.js. It offers built-in props to import languages, but loading all languages increases bundle size significantly.
// react-syntax-highlighter: Built-in language support
import { Prism } from 'react-syntax-highlighter';
// Languages are often bundled or imported specifically
<Prism language="javascript">{code}</Prism>
react-highlight uses highlight.js. It automatically detects languages if configured, but often requires you to load language files globally before use.
// react-highlight: Global language registration
import 'highlight.js/lib/common';
import Highlight from 'react-highlight';
<Highlight className="javascript">{code}</Highlight>
react-codemirror uses CodeMirror modes. You must import the specific mode for each language you support. Theming is done via CodeMirror CSS files.
// react-codemirror: Mode import
import 'codemirror/mode/javascript/javascript';
import 'codemirror/lib/codemirror.css';
<CodeMirror options={{ mode: 'javascript' }} ... />
This is the most critical architectural decision. Three of these packages are for display only. One is for editing.
prism-react-renderer is read-only. It generates static HTML. If you try to add an onChange handler, it will not work because there is no input element.
// prism-react-renderer: No input handling
<Prism code={code} language={language} />
// Cannot capture user typing
react-syntax-highlighter is read-only. It is optimized for rendering speed, not interaction. Modifying the code requires changing the parent state and re-rendering the whole block.
// react-syntax-highlighter: No input handling
<Prism language={language}>{code}</Prism>
// Cannot capture user typing
react-highlight is read-only. It is strictly for presenting code snippets. It does not expose any events for text modification.
// react-highlight: No input handling
<Highlight className={language}>{code}</Highlight>
// Cannot capture user typing
react-codemirror is interactive. It exposes onChange, onFocus, and other editor events. It manages the cursor position and selection state internally.
// react-codemirror: Full input handling
<CodeMirror
value={code}
onChange={newCode => setCode(newCode)}
onFocus={() => console.log('Editor focused')}
/>
Bundle size and rendering performance differ based on how much logic is shipped to the client.
prism-react-renderer is lightweight because it does not include default themes or language definitions. You pay only for what you import. This makes it ideal for performance-critical apps.
// prism-react-renderer: Tree-shaking friendly
// Only import what you use
import { Prism } from 'prism-react-renderer';
react-syntax-highlighter can become heavy if you import all languages or themes. It is easier to set up but requires configuration to avoid bloating the bundle.
// react-syntax-highlighter: Potential bundle bloat
// Importing specific languages is recommended
import { Light } from 'react-syntax-highlighter';
react-highlight depends on highlight.js, which can be large if all languages are included. It does not offer fine-grained tree-shaking controls within the React wrapper itself.
// react-highlight: Full engine load
// Often requires global highlight.js setup
import Highlight from 'react-highlight';
react-codemirror includes the editor engine, which is significantly larger than a highlighter. It is overkill for static snippets but necessary for editing features.
// react-codemirror: Heavy engine
// Includes editor logic, cursor management, and modes
import CodeMirror from 'react-codemirror';
Checking the underlying engine status is vital for long-term projects. Some wrappers rely on older versions of their core libraries.
prism-react-renderer is actively maintained and follows modern React patterns. It works well with server-side rendering and supports React 18 features.
// prism-react-renderer: Modern React support
// Compatible with concurrent features
<Prism code={code} language={language} />
react-syntax-highlighter is maintained but has faced issues with large bundle sizes in the past. It remains a stable choice for standard highlighting needs.
// react-syntax-highlighter: Stable
// Widely used in documentation tools
<Prism language={language}>{code}</Prism>
react-highlight sees less frequent updates. It is stable but may lag behind highlight.js core updates. Use it for simple projects where deep maintenance is not a concern.
// react-highlight: Stable but less active
// Suitable for internal tools
<Highlight className={language}>{code}</Highlight>
react-codemirror wraps CodeMirror 5, which is in maintenance mode. CodeMirror 6 is the current standard. For new projects requiring editing, consider modern wrappers like @uiw/react-codemirror instead.
// react-codemirror: Legacy engine warning
// Uses CodeMirror 5 (CM6 is recommended for new projects)
<CodeMirror value={code} onChange={handleChange} />
| Feature | prism-react-renderer | react-syntax-highlighter | react-highlight | react-codemirror |
|---|---|---|---|---|
| Primary Use | Static Display | Static Display | Static Display | Code Editing |
| Control Level | High (Render Props) | Medium (Component) | Low (Wrapper) | High (Editor API) |
| Engine | Prism.js | Prism or Highlight.js | Highlight.js | CodeMirror 5 |
| Bundle Size | Small (Manual imports) | Medium/Large | Medium/Large | Large |
| Interactivity | None | None | None | Full Input Support |
prism-react-renderer is the choice for teams building design systems or documentation platforms where performance and custom styling are top priorities. It requires more code but gives you full ownership of the output.
react-syntax-highlighter is the practical choice for most applications. It balances ease of use with decent performance. If you need to highlight code quickly without managing tokens, this is the standard path.
react-highlight is suitable for legacy projects or simple internal tools where you already use highlight.js. It is less flexible for modern React patterns but gets the job done with minimal configuration.
react-codemirror is the only option here for editing. However, be aware it uses an older engine. If you need a code editor, evaluate if you can use a CodeMirror 6 wrapper instead for better long-term support.
Final Thought: Do not use an editor package for static display. The performance cost is too high. Conversely, do not try to force a highlighter to act like an editor. Match the tool to the user interaction model.
Choose prism-react-renderer if you need full control over how highlighted tokens are rendered into custom React components. It is ideal for design systems or documentation sites where the default HTML output of other libraries does not match your styling requirements. Be aware that it requires more setup than higher-level wrappers but offers a smaller bundle size.
Choose react-codemirror only if you require a full code editing experience and are comfortable using CodeMirror 5. Note that this package wraps the older CodeMirror version, so for new projects requiring modern features, you should evaluate CodeMirror 6 wrappers instead. It is the only option in this list suitable for user input rather than static display.
Choose react-highlight if you want the simplest possible integration with highlight.js and do not need complex customization. It works well for quick prototypes or internal tools where bundle size is not a critical concern. However, it offers less flexibility than other options if you need to modify the underlying DOM structure.
Choose react-syntax-highlighter if you want a balance between ease of use and feature coverage without managing low-level details. It supports both Prism and highlight.js engines, making it versatile for projects needing support for many languages. It is the best fit for standard blog posts or documentation where you need themes to work out of the box.
A lean Prism highlighter component for React
Comes with everything to render Prismjs syntax highlighted code directly in React & React Native!
Prism React Renderer powers syntax highlighting in the amazing Docusaurus framework and many others.
This library tokenises code using Prism and provides a small render-props-driven component to quickly render it out into React. This is why it even works with React Native! It's bundled with a modified version of Prism that won't pollute the global namespace and comes with a couple of common language syntaxes.
(There's also an escape-hatch to use your own Prism setup, just in case)
It also comes with its own VSCode-like theming format, which means by default you can easily drop in different themes, use the ones this library ships with, or create new ones programmatically on the fly.
(If you just want to use your Prism CSS-file themes, that's also no problem)
This module is distributed via npm which is bundled with node and
should be installed as one of your project's dependencies:
# npm
npm install --save prism-react-renderer
# yarn
yarn add prism-react-renderer
# pnpm
pnpm add prism-react-renderer
Prism React Renderer has a peer dependency on
react
Prism React Renderer has a named export for the <Highlight /> component along with themes. To see Prism React Render in action with base styling check out packages/demo or run pnpm run start:demo from the root of this repository.
import React from "react"
import ReactDOM from "react-dom/client"
import { Highlight, themes } from "prism-react-renderer"
import styles from 'styles.module.css'
const codeBlock = `
const GroceryItem: React.FC<GroceryItemProps> = ({ item }) => {
return (
<div>
<h2>{item.name}</h2>
<p>Price: {item.price}</p>
<p>Quantity: {item.quantity}</p>
</div>
);
}
`
export const App = () => (
<Highlight
theme={themes.shadesOfPurple}
code={codeBlock}
language="tsx"
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre style={style}>
{tokens.map((line, i) => (
<div key={i} {...getLineProps({ line })}>
<span>{i + 1}</span>
{line.map((token, key) => (
<span key={key} {...getTokenProps({ token })} />
))}
</div>
))}
</pre>
)}
</Highlight>
)
ReactDOM
.createRoot(document.getElementById("root") as HTMLElement)
.render(<App />)
By default prism-react-renderer only includes a base set of languages that Prism supports.
Note: Some languages (such as Javascript) are part of the bundle of other languages
Depending on your app's build system you may need to await the import or use require to ensure window.Prism exists before importing the custom languages. You can add support for more by including their definitions from the main prismjs package:
import { Highlight, Prism } from "prism-react-renderer";
(typeof global !== "undefined" ? global : window).Prism = Prism
await import("prismjs/components/prism-applescript")
/** or **/
require("prismjs/components/prism-applescript")
This is the list of props that you should probably know about. There are some advanced props below as well.
Most of these advanced props are included in the defaultProps.
function({})| required
This is called with an object. Read more about the properties of this object in the section "Children Function".
string| required
This is the language that your code will be highlighted as. You can see a list of all languages that are supported out of the box here. Not all languages are included and the list of languages that are currently is a little arbitrary. You can use the escape-hatch to use your own Prism setup, just in case, or add more languages to the bundled Prism.
string| required
This is the code that will be highlighted.
PrismTheme| optional; default is vsDark
If a theme is passed, it is used to generate style props which can be retrieved via the prop-getters which are described in "Children Function".
Read more about how to theme prism-react-renderer in
the section "Theming".
prism| optional; default is the vendored version
This is the Prismjs library itself.
A vendored version of Prism is provided (and also exported) as part of this library.
This vendored version doesn't pollute the global namespace, is slimmed down,
and doesn't conflict with any installation of prismjs you might have.
If you're only using Prism.highlight you can choose to use prism-react-renderer's
exported, vendored version of Prism instead.
But if you choose to use your own Prism setup, simply pass Prism as a prop:
// Whichever way you're retrieving Prism here:
import Prism from 'prismjs/components/prism-core';
<Highlight prism={Prism} {/* ... */} />
This is where you render whatever you want to based on the output of <Highlight />.
You use it like so:
const ui = (
<Highlight>
{highlight => (
// use utilities and prop getters here, like highlight.className, highlight.getTokenProps, etc.
<pre>{/* more jsx here */}</pre>
)}
</Highlight>
);
The properties of this highlight object can be split into two categories as indicated below:
These properties are the flat output of <Highlight />. They're generally "state" and are what
you'd usually expect from a render-props-based API.
| property | type | description |
|---|---|---|
tokens | Token[][] | This is a doubly nested array of tokens. The outer array is for separate lines, the inner for tokens, so the actual content. |
className | string | This is the class you should apply to your wrapping element, typically a <pre> |
A "Token" is an object that represents a piece of content for Prism. It has a types property, which is an array
of types that indicate the purpose and styling of a piece of text, and a content property, which is the actual
text.
You'd typically iterate over tokens, rendering each line, and iterate over its items, rendering out each token, which is a piece of
this line.
These functions are used to apply props to the elements that you render. This gives you maximum flexibility to render what, when, and wherever you like.
You'd typically call these functions with some dictated input and add on all other props that it should pass through. It'll correctly override and modify the props that it returns to you, so passing props to it instead of adding them directly is advisable.
| property | type | description |
|---|---|---|
getLineProps | function({}) | returns the props you should apply to any list of tokens, i.e. the element that contains your tokens. |
getTokenProps | function({}) | returns the props you should apply to the elements displaying tokens that you render. |
getLinePropsYou need to add a line property (type: Token[]) to the object you're passing to
getLineProps.
This getter will return you props to spread onto your line elements (typically <div>s).
It will typically return a className (if you pass one it'll be appended), children,
style (if you pass one it'll be merged). It also passes on all other props you pass
to the input.
The className will always contain .token-line.
getTokenPropsYou need to add a token property (type: Token) to the object you're passing to
getTokenProps.
This getter will return you props to spread onto your token elements (typically <span>s).
It will typically return a className (if you pass one it'll be appended), children,
style (if you pass one it'll be merged). It also passes on all other props you pass
to the input.
The className will always contain .token. This also provides full compatibility with
your old Prism CSS-file themes.
useTokenize
(options: TokenizeOptions) => Token[][]
type TokenizeOptions = {
prism: PrismLib
code: string
grammar?: PrismGrammar
language: Language
}
This is a React hook that tokenizes code using Prism. It returns an array of tokens that can be rendered using the built-in <Highlight /> component or your own custom component. It uses normalizeTokens internally to convert the tokens into a shape that can be rendered.
prism: PrismLib: the Prism library to use for tokenization. This can be the vendored version of Prism that is included with prism-react-renderer or a custom version of Prism that you have configured.
code: string: a string containing the code to tokenize.
grammar?: PrismGrammar: a Prism grammar object to use for tokenization. If this is omitted, the tokens will just be normalized. A grammar can be obtained from Prism.languages or by importing a language from prismjs/components/.
language: Language: the language to use for tokenization. This should be a language that Prism supports.
normalizeTokens
(tokens: (PrismToken | string)[]) => Token[][]
Takes an array of Prism’s tokens and groups them by line, converting strings into tokens. Tokens can become recursive in some cases which means that their types are concatenated. Plain-string tokens however are always of type plain.
PrismToken is an internal alias for Token exported by prismjs and is defined here.
Token is an internal object that represents a slice of tokenized content for Prism with three properties:
types: string[]: an array of types that indicate the purpose and styling of a piece of textcontent: string: the content of the tokenempty: boolean: a flag indicating whether the token is empty or not.The defaultProps you'd typically apply in a basic use-case, contain a default theme.
This theme is vsDark.
While all classNames are provided with <Highlight />, so that you could use your good
old Prism CSS-file themes, you can also choose to use prism-react-renderer's themes like so:
import { Highlight, themes } from 'prism-react-renderer';
<Highlight theme={themes.dracula} {/* ... */} />
These themes are JSON-based and are heavily inspired by VSCode's theme format.
Their syntax, expressed in Flow looks like the following:
{
plain: StyleObj,
styles: Array<{
types: string[],
languages?: string[],
style: StyleObj
}>
}
The plain property provides a base style-object. This style object is directly used
in the style props that you'll receive from the prop getters, if a theme prop has
been passed to <Highlight />.
The styles property contains an array of definitions. Each definition contains a style
property, that is also just a style object. These styles are limited by the types
and languages properties.
The types properties is an array of token types that Prism outputs. The languages
property limits styles to highlighted languages.
When converting a Prism CSS theme it's mostly just necessary to use classes as
types and convert the declarations to object-style-syntax and put them on style.
If you are migrating from v1.x to v2.x, follow these steps
- import Highlight, { defaultProps } from "prism-react-renderer";
+ import { Highlight } from "prism-react-renderer"
const Content = (
- <Highlight {...defaultProps} code={exampleCode} language="jsx">
+ <Highlight code={exampleCode} language="jsx">
- const theme = require('prism-react-renderer/themes/github')
+ const theme = require('prism-react-renderer').themes.github
By default prism-react-renderer only includes a base set of languages that Prism supports. Depending on your app's build system you may need to await the import or use require to ensure window.Prism exists before importing the custom languages.
See: https://github.com/FormidableLabs/prism-react-renderer#custom-language-support
Install prismjs (if not available yet):
# npm
npm install --save prismjs
# yarn
yarn add prismjs
# pnpm
pnpm add prismjs
If the language is not already bundled in the above, you can add additional languages with the following code:
import { Highlight, Prism } from "prism-react-renderer";
(typeof global !== "undefined" ? global : window).Prism = Prism
await import("prismjs/components/prism-applescript")
/** or **/
require("prismjs/components/prism-applescript")
MIT
Active: Nearform is actively working on this project, and we expect to continue work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.