Gathering detailed insights and metrics for react-html-parser
Gathering detailed insights and metrics for react-html-parser
Gathering detailed insights and metrics for react-html-parser
Gathering detailed insights and metrics for react-html-parser
Converts HTML strings directly into React components and provide a simple way to modify and replace the content.
npm install react-html-parser
Typescript
Module System
Node Version
NPM Version
96.9
Supply Chain
99.3
Quality
74.4
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
64,924,754
Last Day
7,402
Last Week
253,850
Last Month
1,116,719
Last Year
12,826,167
MIT License
795 Stars
61 Commits
104 Forks
9 Watchers
17 Branches
3 Contributors
Updated on May 23, 2025
Minified
Minified + Gzipped
Latest Version
2.0.2
Package Id
react-html-parser@2.0.2
Size
603.97 kB
NPM Version
5.5.1
Node Version
8.9.1
Published on
Nov 29, 2017
Cumulative downloads
Total Downloads
Last Day
4.7%
7,402
Compared to previous day
Last Week
-10.8%
253,850
Compared to previous week
Last Month
5.5%
1,116,719
Compared to previous month
Last Year
2.6%
12,826,167
Compared to previous year
1
1
28
A utility for converting HTML strings into React components. Avoids the use of dangerouslySetInnerHTML and converts standard HTML elements, attributes and inline styles into their React equivalents.
1npm install react-html-parser 2# or 3yarn add react-html-parser
1import React from 'react'; 2import ReactHtmlParser, { processNodes, convertNodeToElement, htmlparser2 } from 'react-html-parser'; 3 4class HtmlComponent extends React.Component { 5 render() { 6 const html = '<div>Example HTML string</div>'; 7 return <div>{ ReactHtmlParser(html) }</div>; 8 } 9}
function ReactHtmlParser(html, [options])
Takes an HTML string and returns equivalent React elements
1import ReactHtmlParser from 'react-html-parser';
html
: The HTML string to parseoptions
: Options object
htmlparser2
The transform function will be called for every node that is parsed by the library.
function transform(node, index)
node
: The node being parsed. This is the htmlparser2 node object. Full details can be found on their project page but important properties are:
type
(string): The type of node (tag, text, style etc)name
(string): The name of the nodechildren
(array): Array of children nodesnext
(node): The node's next siblingprev
(node): The node's previous siblingparent
(node): The node's parentdata
(string): The text content, if the type
is textindex
(number): The index of the node in relation to it's parentreturn null
Returning null will prevent the node and all of it's children from being rendered.
1function transform(node) { 2 // do not render any <span> tags 3 if (node.type === 'tag' && node.name === 'span') { 4 return null; 5 } 6}
return undefined
If the function does not return anything, or returns undefined, then the default behaviour will occur and the parser will continue was usual.
return React element
React elements can be returned directly
1import React from 'react'; 2function transform(node) { 3 if (node.type === 'tag' && node.name === 'b') { 4 return <div>This was a bold tag</div>; 5 } 6}
Allows pre-processing the nodes generated from the html by htmlparser2
before being passed to the library and converted to React elements.
function preprocessNodes(nodes)
nodes
: The entire node tree generated by htmlparser2
.The preprocessNodes
function should return a valid htmlparser2
node tree.
function convertNodeToElement(node, index, transform)
Processes a node and returns the React element to be rendered. This function can be used in conjunction with the previously described transform
function to continue to process a node after modifying it.
1import { convertNodeToElement } from 'react-html-parser';
node
: The node to processindex
(number): The index of the node in relation to it's parenttransform
: The transform function as described above1import { convertNodeToElement } from 'react-html-parser'; 2function transform(node, index) { 3 // convert <ul> to <ol> 4 if (node.type === 'tag' && node.name === 'ul') { 5 node.name = 'ol'; 6 return convertNodeToElement(node, index, transform); 7 } 8}
htmlparser2
The library exposes the htmlparser2
library it uses. This allows consumers
to use it without having to add it as a separate dependency.
See https://github.com/fb55/htmlparser2 for full details.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/22 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
138 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-02
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More