Registry / serialization / html-react-parser

html-react-parser

JSON →
library6.0.1jsnpmunverified

html-react-parser is a utility library designed to convert HTML strings into React elements, making it suitable for rendering dynamic content securely and efficiently within React applications. The current stable version is 6.0.1. The package maintains a fairly active release cadence, with frequent patch updates addressing dependency bumps and minor fixes, alongside major versions introducing breaking changes, typically related to underlying parser updates or build configurations. A key differentiator is its ability to operate seamlessly in both Node.js (server-side rendering) and browser environments. It provides powerful customization options like the `replace` and `transform` functions, allowing developers fine-grained control over how HTML nodes are converted and rendered, including advanced use cases like sanitization or replacing specific elements with custom React components. It ships with TypeScript types for improved developer experience.

npm install html-react-parser
INSTALL
IMPORT
SIG · HTML-REACT-PARSER
H
html-react-parser
serializationjavascriptv6.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
import parse from 'html-react-parser';
const parse = require('html-react-parser');
The primary function is a default export. For CommonJS, use `require('html-react-parser')` directly.
HTMLReactParserOptions
import type { HTMLReactParserOptions } from 'html-react-parser';
Import the type definition for the parser options when working with TypeScript to ensure type safety for the `options` object.
Element
import type { Element } from 'html-react-parser';
Import specific DOM node types like `Element` if you are implementing custom `replace` or `transform` functions with TypeScript and need to assert node types.

Demonstrates parsing a complex HTML string into React elements, including basic usage and advanced replacement logic for specific tags.

import parse from 'html-react-parser'; import React from 'react'; import ReactDOM from 'react-dom/client'; const htmlString = ` <div> <h1>Welcome to My Blog!</h1> <p>This is a paragraph with <strong>bold text</strong> and an <a href="#">example link</a>.</p> <ul> <li>Item 1</li> <li>Item 2</li> </ul> <img src="https://via.placeholder.com/150" alt="Placeholder Image" /> <p style="color: blue;">This text is blue.</p> </div> `; const App = () => ( <div> <h2>Parsed HTML Content:</h2> {parse(htmlString, { replace: (node) => { // Example: Replace 'a' tags with custom component or modify props if (node.type === 'tag' && node.name === 'a') { return ( <a {...node.attribs} style={{ color: 'red' }}> {node.children ? parse(node.children.map(child => child.data || child.children?.map(c => c.data).join('') || '').join('')) : ''} </a> ); } // Example: Remove 'img' tags if (node.type === 'tag' && node.name === 'img') { return <p>Image removed for security reasons.</p>; } return node; // Return the node to be parsed normally if no replacement is needed }, })} </div> ); const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement); root.render(<App />);
Debug
Known issues
breakingVersion 6.0.0 introduced breaking changes by bumping the underlying `html-dom-parser` from 5.1.8 to 7.0.0 and `domhandler` from 5.0.3 to 6.0.1. These dependency updates may alter how certain malformed HTML is parsed or how DOM nodes are structured internally.
fix
Review your HTML input and custom `replace` or `transform` functions, especially if you rely on specific behaviors of older `html-dom-parser` or `domhandler` versions. Test thoroughly after upgrading.
affects: >=6.0.0
breakingThe build target for html-react-parser changed from `es5` to `es2016` in version 6.0.0. This means the distributed code will target a more modern JavaScript environment.
fix
Ensure your project's build tooling (e.g., Babel, Webpack) is configured to transpile `es2016` syntax down to your desired target environment (e.g., `es5` for older browsers) if necessary.
affects: >=6.0.0
gotchaThis library is not XSS-safe by default. If you parse untrusted HTML, malicious scripts or attributes could be injected into your React tree, leading to XSS vulnerabilities.
fix
Always sanitize untrusted HTML before passing it to `html-react-parser` using a dedicated sanitization library (e.g., `dompurify`). Alternatively, implement a robust `replace` function to explicitly filter out dangerous tags and attributes.
affects: >=1.0.0
gotchaBy default, `<script>` tags are parsed but not executed by React. However, if they contain certain attributes or are part of custom replacement logic, they might still pose a security risk.
fix
For untrusted HTML, explicitly remove all `<script>` tags using the `replace` option or a sanitization library before parsing to prevent any potential execution or manipulation of the DOM.
affects: >=1.0.0
Errors
Common errors & fixes
TS Error: Property 'attribs' does not exist on type 'DOMNode'
In TypeScript, the `DOMNode` type returned in the `replace` or `transform` functions is a union type that includes various node types (text, comment, element). The `attribs` property only exists on `Element` nodes.
fix
You must narrow the type of `node` to `Element` before accessing `node.attribs`. Use a type guard like `if (node.type === 'tag')` to ensure `node` is an HTML element.
Elements aren't nested correctly or attributes aren't getting called (e.g., `onclick` not firing)
React handles DOM events differently than plain HTML. It uses a synthetic event system, and some HTML attributes (like `onclick`) are not directly mapped to React props or are case-sensitive (e.g., `onClick` in React). Incorrect HTML structure can also lead to parsing issues.
fix
Ensure your HTML is well-formed. For event handlers, you often need to use React's camelCase synthetic events (e.g., `onClick` instead of `onclick`) and pass a function reference. For complex interactions, use the `replace` option to convert problematic HTML elements into proper React components with their own event handlers.
Upgrade
Version history
6.0.1latest on npm
Audit
Dependencies
@types/reactoptionalTypeScript types for React, required when using TypeScript in projects targeting various React versions.
reactrequiredCore dependency for creating and rendering React elements.
Agent activity
6 hits · last 30 days
node
6
Resources