Skip to content
Converts HTML strings directly into React components avoiding the need to use dangerouslySetInnerHTML
Branch: master
Clone or download
Latest commit f28597e Nov 29, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
demo Update demo site to react 16 Oct 1, 2017
scripts Update attributes list with latest additions and removals from react … Aug 9, 2017
src Fix empty string styles by running inlineStyleToObject on all strings… Nov 29, 2017
test Add integration and unit test for empty string style prop value in ge… Nov 29, 2017
.babelrc Add options argument to ReactHtmlParser function (#21) Aug 9, 2017
.eslintrc Add options argument to ReactHtmlParser function (#21) Aug 9, 2017
.gitignore Initial commit Jan 19, 2016
.npmignore Ignore .idea file Jan 21, 2016
.travis.yml Update changelog Nov 29, 2017 Add transform function option (#24) Aug 22, 2017 Add downloads badge to readme Nov 1, 2017
karma.conf.js Add karma-es6-shim for ES6 support (React16 uses Set) for phantomJS Nov 29, 2017
package-lock.json 2.0.2 Nov 29, 2017
package.json 2.0.2 Nov 29, 2017
tests.webpack.js Add transform function option (#24) Aug 22, 2017
webpack.config.base.js Initial commit Jan 19, 2016
webpack.config.development.js Initial commit Jan 19, 2016
webpack.config.production.js Initial commit Jan 19, 2016
webpack.config.test.js Initial commit Jan 19, 2016

React HTML Parser

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.

Try the Live Demo

Travis branch Coveralls npm Downloads David


npm install react-html-parser
# or
yarn add react-html-parser


import React from 'react';
import ReactHtmlParser, { processNodes, convertNodeToElement, htmlparser2 } from 'react-html-parser';

class HtmlComponent extends React.Component {
  render() {
    const html = '<div>Example HTML string</div>';
    return <div>{ ReactHtmlParser(html) }</div>;


function ReactHtmlParser(html, [options])

Takes an HTML string and returns equivalent React elements


import ReactHtmlParser from 'react-html-parser';


  • html: The HTML string to parse
  • options: Options object
    • decodeEntities=true (boolean): Whether to decode html entities (defaults to true)
    • transform (function): Transform function that is applied to every node
    • preprocessNodes (function): Pre-process the nodes generated by htmlparser2

Transform Function

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 node
    • children (array): Array of children nodes
    • next (node): The node's next sibling
    • prev (node): The node's previous sibling
    • parent (node): The node's parent
    • data (string): The text content, if the type is text
  • index (number): The index of the node in relation to it's parent

Return Types

return null Returning null will prevent the node and all of it's children from being rendered.

function transform(node) {
  // do not render any <span> tags
  if (node.type === 'tag' && === 'span') {
    return null;

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

import React from 'react';
function transform(node) {
  if (node.type === 'tag' && === 'b') {
    return <div>This was a bold tag</div>;

preprocessNodes Function

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.
Return type

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.


import { convertNodeToElement } from 'react-html-parser';


  • node: The node to process
  • index (number): The index of the node in relation to it's parent
  • transform: The transform function as described above
import { convertNodeToElement } from 'react-html-parser';
function transform(node, index) {
  // convert <ul> to <ol>
  if (node.type === 'tag' && === 'ul') { = 'ol';
    return convertNodeToElement(node, index, transform);


The library exposes the htmlparser2 library it uses. This allows consumers to use it without having to add it as a separate dependency.

See for full details.

You can’t perform that action at this time.