Prism React Renderer

2.4.1 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the <Highlight /> component with a built-in theme and custom rendering logic.

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 />)

view raw JSON →