Prism React Renderer
Prism React Renderer is a lightweight, render-props-driven React component that facilitates syntax highlighting of code using a bundled and modified version of PrismJS. It is currently stable at version 2.4.1 and is actively maintained by FormidableLabs, with regular updates to support new React features and fix bugs. The library distinguishes itself by not polluting the global namespace with PrismJS, supporting React Native out-of-the-box, and offering a VSCode-like theming format that allows for easy theme integration or programmatic theme creation. It powers syntax highlighting in frameworks like Docusaurus and provides utilities for custom language support and token normalization, making it a flexible choice for displaying code snippets.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'languages')
cause The Prism instance is not correctly loaded or accessible, often when attempting to highlight a language that isn't registered or if the custom Prism prop is misconfigured.fixEnsure the `language` prop is a valid Prism language. If using a custom Prism instance, verify it's correctly passed to the `prism` prop. Check for `prism-react-renderer`'s bundled languages or add custom ones via the escape-hatch. -
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
cause Often indicates that the `Highlight` component was not imported correctly, or there's a problem with the React environment.fixVerify the import statement: `import { Highlight } from 'prism-react-renderer'`. Ensure React and ReactDOM are properly installed and configured for your project. -
Unhandled Rejection (TypeError): theme is undefined
cause The `theme` prop received an `undefined` value, which can happen if a theme isn't found or loaded correctly.fixUse one of the `themes` exported by `prism-react-renderer` (e.g., `themes.shadesOfPurple`) or ensure your custom theme object is valid. Version 2.0.5 fixed a bug where an undefined theme would cause a runtime error, so ensure you are on a recent version if this issue persists.
Warnings
- breaking The `theme dictionary hook` was removed in v2.4.1. If you were using this internal hook, your code will break.
- breaking Upgrading from v1 to v2 involved significant changes. Specific breaking changes are documented in the upgrade guide.
- gotcha Prism React Renderer requires `react` as a peer dependency. Ensure you have `react` installed in your project at a compatible version.
- gotcha When using custom Prism setups, ensure the `prism` prop is correctly passed to the `Highlight` component to avoid issues with language definitions or tokenization.
- gotcha Since v2.4.1, React Server Components (RSC) are supported. If deploying to an RSC environment, ensure your setup is compatible with this change.
Install
-
npm install prism-react-renderer -
yarn add prism-react-renderer -
pnpm add prism-react-renderer
Imports
- Highlight
const { Highlight } = require('prism-react-renderer')import { Highlight } from 'prism-react-renderer' - themes
import themes from 'prism-react-renderer/themes'
import { themes } from 'prism-react-renderer' - normalizeTokens
import { normalizeTokens } from 'prism-react-renderer' - useTokenize
import { useTokenize } from 'prism-react-renderer'
Quickstart
import React from "react"
import ReactDOM from "react-dom/client"
import { Highlight, themes } from "prism-react-renderer"
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="typescript"
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre style={style} className={className}>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })}>
{line.map((token, key) => (
<span {...getTokenProps({ token, key })} />
))}
</div>
))}
</pre>
)}
</Highlight>
);
ReactDOM.createRoot(document.getElementById('root')!).render(<App />)