rehype-react is a `rehype` plugin designed to compile HTML abstract syntax trees (hast) into JSX elements, enabling the rendering of HTML content within various JSX runtimes. It currently supports React, Preact, Solid, Svelte, and Vue. The current stable version is 8.0.0. The project maintains an active release cadence with frequent patch and minor updates, and major versions are released to introduce significant architectural changes, such as the move to ESM or multi-framework support. A key differentiator is its integration within the `unified` ecosystem, allowing developers to leverage a rich pipeline of AST transformations on HTML content before it's rendered. This provides more control compared to simpler solutions like `react-markdown` and offers an alternative to `react-remark` for HTML processing.
npm install rehype-reactVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `rehype-react` within a React component to transform an HTML string into a React element tree, supporting multiple JSX runtimes since v8.0.0.
Upgrade your Node.js environment to version 16 or newer. Use `nvm install 16` or `nvm use 16` if you use nvm.
Migrate your project to use ESM (`import` statements) or ensure that your build system correctly transpiles ESM for CommonJS consumption. For Node.js, add `"type": "module"` to your `package.json`.
Change your `rehypeReact` usage from `.use(rehypeReact, {Fragment, createElement})` to `.use(rehypeReact, {Fragment: prod.Fragment, jsx: prod.jsx, jsxs: prod.jsxs})` after importing `import * as prod from 'react/jsx-runtime'`.Update your plugin options from `fixTableCellAlign: true` to `tableCellAlignToStyle: true`.
Review your TypeScript code for type errors after upgrading and adjust types or cast as necessary, especially around the `file.result` type or custom component props.
Change `const rehypeReact = require('rehype-react')` to `import rehypeReact from 'rehype-react'` and ensure your project is configured for ESM (e.g., by adding `"type": "module"` to `package.json`).Instead of `{Fragment, createElement}`, pass an object containing the JSX runtime functions, usually imported as `import * as production from 'react/jsx-runtime'` and then `.use(rehypeReact, production)`.Rename the option `fixTableCellAlign` to `tableCellAlignToStyle` in your plugin configuration.
Ensure your build tools correctly polyfill Node.js globals for browser contexts, or conditionally access `process.version` only in Node.js environments. For example, use `typeof process !== 'undefined' ? process.version : 'browser'`.