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.
npm install react-html-parser
import React from 'react';
import ReactHtmlParser from 'react-html-parser';
class HtmlComponent extends React.Component {
render() {
const html = '<div>Example HTML string</div>';
const options = {
decodeEntities: true
};
return <div>{ ReactHtmlParser(html, options) }</div>;
}
}
Takes an HTML string and returns equivalent React elements
Arguments
- html: The HTML string to parse
- options: Options object
- decodeEntities=true (boolean): Whether to decode html entities (defaults to true)