react-html-parser is a utility library designed for converting HTML strings into a tree of React components, thereby circumventing the security risks associated with directly using React's `dangerouslySetInnerHTML` property. The library, currently at version 2.0.2, provides a programmatic way to parse HTML content. While its release cadence has been infrequent since its v2.0.0 release in late 2017, it has received recent maintenance updates (v2.0.1 and v2.0.2 in 2020 and 2021 respectively), suggesting it's actively maintained for critical issues rather than undergoing active feature development. A key differentiator is its reliance on `htmlparser2` for robust HTML parsing and its extensive API, which includes `transform` and `preprocessNodes` functions. These functions allow developers fine-grained control over how nodes are processed and rendered, facilitating custom component mapping, attribute manipulation, and node filtering. It also automatically handles the conversion of standard HTML attributes and inline styles to their React equivalents.
npm install react-html-parserVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to parse an HTML string into React components using `ReactHtmlParser`, including the use of a `transform` function to modify elements (e.g., adding `target="_blank"` to external links) or prevent rendering of specific tags like `script` for security. It showcases handling attributes and passing options.
Sanitize all untrusted or user-generated HTML input using a robust library such as `DOMPurify` before passing it to `ReactHtmlParser`.
Ensure your project's React version is within the compatible range. If using npm v7+, `npm install --legacy-peer-deps` might temporarily resolve installation errors but is not recommended for production without careful consideration of potential incompatibilities.
If the old behavior of preserving HTML entities is desired, set the `decodeEntities` option to `false`: `ReactHtmlParser(html, { decodeEntities: false })`.Manually wrap these tags in `div` elements within your HTML string if the previous structure is essential, or adjust your CSS and layout expectations to account for the direct rendering of these tags.
Upgrade to `react-html-parser@1.0.2` or a later version to ensure correct rendering of void elements and boolean attributes.
Change your import statement from `import ReactHtmlParser from 'react-html-parser';` to `import { ReactHtmlParser } from 'react-html-parser';`.Adjust your project's `react` version to be compatible (e.g., `react@16`). If absolutely necessary and aware of potential incompatibilities, you might use `npm install --legacy-peer-deps` (for npm v7+) but this is not a recommended long-term solution.
Ensure the `decodeEntities` option is set to `true` (which is the default behavior since `v2.0.0`), or upgrade to `v2.0.0` or higher to benefit from default entity decoding. For `v1.x`, you would need to manually decode entities before passing the HTML string.