react-refractor is a lightweight React component for syntax highlighting code snippets, acting as a thin wrapper around the `refractor` library. `refractor` itself is a virtual DOM implementation of `Prism.js`, allowing for efficient updates and server-side rendering without direct DOM manipulation. The current stable version is v4.0.0, which dropped Node.js 18 support and upgraded to `refractor` v5. The library maintains a moderately active release cadence, with several minor and major versions released within the last year. A key differentiator is its VDOM-based approach, which makes it performant and flexible for React environments but also means it's incompatible with `Prism.js` plugins. Developers must explicitly import and register specific language syntaxes from `refractor` to keep bundle sizes small, and styling is left to the developer, often by importing `Prism.js` themes. It requires React 18+ and is ESM-only since v3.0.0.
npm install react-refractorVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to install `react-refractor`, register multiple language syntaxes (JavaScript, PHP, CSS) from `refractor`, and render code snippets using the `Refractor` component. It highlights the explicit language registration process and the component's basic usage with `language` and `value` props.
Upgrade Node.js environment to version 20 or higher. Ensure `refractor` v5 is compatible with your project if you're directly interacting with it.
Migrate your project to use ES Modules (`import ... from '...'`) or ensure your build system correctly handles ESM. Update all imports from `import Refractor from 'react-refractor'` to `import { Refractor } from 'react-refractor'`.Upgrade your React installation to version 18 or newer. Ensure your project's build targets ES6 or newer.
If `Prism.js` plugin functionality is crucial, `react-refractor` might not be the right choice. Consider alternative highlighting libraries that directly integrate `Prism.js`.
Import a Prism.js-compatible CSS theme (e.g., from `prismjs/themes`) into your project. You can also customize your own CSS to target the classes generated by `refractor`.
Refactor your imports to use ES Modules syntax: `import { Refractor } from 'react-refractor';`. Ensure your project's `package.json` has `"type": "module"` or your build system handles ESM correctly.Change your import statement to use named exports: `import { Refractor } from 'react-refractor';`.Import the required language module (e.g., `import js from 'refractor/lang/javascript';`) and then register it: `registerLanguage(js);`.