You probably want to look deeper into dangerouslySetInnerHTML. Here is an example how to render HTML from a string in a React component:

import React from 'react';
import { render } from 'react-dom';

const htmlString = '<h1>Hello World! </h1>';

const App = () => (
  <div dangerouslySetInnerHTML={{ __html: htmlString }} />
);

render(<App />, document.getElementById('root'));

Full example here: https://codesandbox.io/s/xv40xXQzE

Read more about dangerouslySetInnerHTML in the React docs here: https://facebook.github.io/react/docs/dom-elements.html#dangerouslysetinnerhtml

Answer from glennreyes on Stack Overflow
🌐
npm
npmjs.com › package › html-react-parser
html-react-parser - npm
1 month ago - 2 export { Parser, type ParserOptions }; ~~~~~~~~~~~~~ Then upgrade to the latest version of typescript. See #748. ... html-to-react - Single x 1,270,990 ops/sec ±0.21% (100 runs sampled) html-to-react - Multiple x 522,472 ops/sec ±0.64% (94 runs sampled) html-to-react - Complex x 54,028 ops/sec ±0.82% (98 runs sampled)
      » npm install html-react-parser
    
Published   Apr 08, 2026
Version   6.0.1
Top answer
1 of 7
57

You probably want to look deeper into dangerouslySetInnerHTML. Here is an example how to render HTML from a string in a React component:

import React from 'react';
import { render } from 'react-dom';

const htmlString = '<h1>Hello World! </h1>';

const App = () => (
  <div dangerouslySetInnerHTML={{ __html: htmlString }} />
);

render(<App />, document.getElementById('root'));

Full example here: https://codesandbox.io/s/xv40xXQzE

Read more about dangerouslySetInnerHTML in the React docs here: https://facebook.github.io/react/docs/dom-elements.html#dangerouslysetinnerhtml

2 of 7
30

As pointed out in this answer by EsterlingAccimeYoutuber, you can use a parser in case you don't want to use dangerouslySetInnerHTML attribute.

By now, react-html-parser has not been updated for 3 years, so I went looking for a different module.

html-react-parser does same job but is frequently maintained and updated.

It should be good practice to sanitize your html-String to prevent XSS attacks. dompurify can be used for that.

I updated EsterlingAccimeYoutuber's code-example to the following:

import React from 'react';
import { render } from 'react-dom';
import parse from 'html-react-parser';
import DOMPurify from 'dompurify';

const SpecialButton = ({ children, color }) => (
  <button style={{color}}>{children}</button>
);

const htmlFromCMS = `
<div>Hi, 
  <SpecialButton color="red">My Button</SpecialButton>
</div>`;

const htmlFrom = (htmlString) => {
        const cleanHtmlString = DOMPurify.sanitize(htmlString,
          { USE_PROFILES: { html: true } });
        const html = parse(cleanHtmlString);
        return html;
}

const App = () => (
  <div>
     {htmlFromCMS && htmlFrom(htmlFromCMS)}
  </div>
);


render(<App />, document.getElementById('root'));

Inspired by original post above, hence special thanks to original authors!

🌐
npm Trends
npmtrends.com › html-react-parser-vs-react-html-parser-vs-react-render-html
html-react-parser vs react-html-parser vs react-render-html | npm trends
Comparing trends for html-react-parser 6.0.1 which has 3,344,817 weekly downloads and 2,404 GitHub stars vs. react-html-parser 2.0.2 which has 241,816 weekly downloads and 797 GitHub stars vs. react-render-html 0.6.0 which has 44,623 weekly downloads and 214 GitHub stars.
🌐
GitHub
github.com › remarkablemark › html-react-parser
GitHub - remarkablemark/html-react-parser: 📝 HTML to React parser.
2 export { Parser, type ParserOptions }; ~~~~~~~~~~~~~ Then upgrade to the latest version of typescript. See #748. ... html-to-react - Single x 1,270,990 ops/sec ±0.21% (100 runs sampled) html-to-react - Multiple x 522,472 ops/sec ±0.64% (94 runs sampled) html-to-react - Complex x 54,028 ops/sec ±0.82% (98 runs sampled)
Starred by 2.4K users
Forked by 140 users
Languages   TypeScript 87.9% | JavaScript 12.1%
🌐
npm
npmjs.com › package › react-html-parser
react-html-parser - npm
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) ... 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
      » npm install react-html-parser
    
Published   Nov 29, 2017
Version   2.0.2
Author   Peter Newnham
🌐
Medium
medium.com › @vienpham2019 › react-html-parser-7bf4286b38f9
React HTML parser. HTML to React parser that works on both… | by Vienpham | Medium
May 18, 2020 - Avoids the use of dangerouslySetInnerHTML and converts standard HTML elements, attributes and inline styles into their React equivalents.
Top answer
1 of 1
8

I've recently been studying this issue for a Headless CMS project I'm working on. From what I understand:

dangerouslySetInnerHtml creates DOM elements outside of ReactDOM.Render() method, so it's not dynamically maintained by the React library. This basically defeats the purpose of using React in the first place (displaying and maintaining virtual DOM).

More concerning, though, is that it's vulnerable to Cross-Site Scripting (XSS) attacks, which is where it gets its name. These are a very common form of attack on the web. You can read about that here: https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml

If you want the app to be less prone to attacks, you'll have to use a sanitization library like DOMPurify for dangerouslySetInnerHtml, so you're likely to have another dependency either way. Once you compile the app for production (npm build) the minimization process makes the codebase extremely compact, and you can do some optimization beforehand with techniques like code-splitting, which makes each section of your page load only if requested, instead of all at once: https://reactjs.org/docs/code-splitting.html

I wouldn't worry too much about a few dependencies, personally - they're a fact of life on the modern web. I've been leaning towards using html-react-parser, but I caveat that by saying I have not investigated whether it reduces XSS vulnerability. However, even if both are vulnerable to XSS attacks, at least html-react-parser introduces the elements through ReactDOM.render() so they don't make the DOM all catty-wompus - that sounds like a recipe for disaster down the road.

🌐
JSFiddle
jsfiddle.net › remarkablemark › 7v86d800
html-react-parser example - JSFiddle - Code Playground
Adding External Resources will no longer create a list of resources in the sidebar but will be injected as a LINK or SCRIPT tag inside of the HTML panel.
Find elsewhere
🌐
DEV Community
dev.to › joshydev › safely-handling-html-in-react-ba
Safely Handling HTML in React - DEV Community
June 21, 2024 - Use html-react-parser: If you need safer HTML parsing with transformation capabilities and can afford the performance and bundle size trade-offs.
🌐
DEV Community
dev.to › kalush89 › how-to-parse-html-string-in-react-53fh
How to Parse HTML string in React - DEV Community
February 21, 2023 - react-html-parser According to this npm package's Readme, it converts standard HTML elements, attributes, and inline styles into their React equivalents
🌐
CodeSandbox
codesandbox.io › s › html-react-parser-940pov1l4w
html-react-parser - CodeSandbox
February 13, 2024 - CodeSandbox is a cloud development platform that empowers developers to code, collaborate and ship projects of any size from any device in record time.
Published   Jan 08, 2019
Author   remarkablemark
🌐
Medium
medium.com › @san.vuthy08 › html-react-parser-1d0df932303a
HTML REACT PARSER. An HTML to React parser that works on… | by San Vuthy | Medium
May 20, 2019 - Although <script> tags are parsed, react-dom does not render the contents. See #98. That’s because inline event handlers like onclick are parsed as a string rather than a function. See #73. ... html-to-react - Single x 415,186 ops/sec ±0.92% (85 runs sampled) html-to-react - Multiple x 139,780 ops/sec ±2.32% (87 runs sampled) html-to-react - Complex x 8,118 ops/sec ±2.99% (82 runs sampled)
🌐
Snyk
snyk.io › advisor › html-react-parser › html-react-parser code examples
Top 5 html-react-parser Code Examples | Snyk
April 19, 2022 - domNode, 'attribs[data-gts-swapped-href]', null ); // replaces local links with element if ( domNode.name === 'a' &amp;&amp; !wasLinkProcessed &amp;&amp; elementUrlNoProtocol.includes(wordPressUrlNoProtocol) &amp;&amp; !elementUrlNoProtocol.includes(uploadsUrlNoProtocol) ) { let url = urlParsed.path(); return ( {domToReact(domNode.children, parserOptions)} ); } // cleans up internal processing attribute if (wasLinkProcessed) { delete domNode.attribs['data-gts-swapped-href']; } // data passed from sourceParser const fluidData = domNode.name === 'img' &amp;&amp; getByPath(domNode, 'attribs[data-gts-encfluid]', null); if (fluidData) { const fluidDataParsed = JSON.parse(fluidData);
🌐
NPM Compare
npm-compare.com › html-react-parser,react-html-parser,react-markdown
react-markdown vs html-react-parser vs react-html-parser | HTML and Markdown Parsing in React Comparison
They often include features like ... and Markdown elements. html-react-parser is a lightweight library for parsing HTML strings and converting them into React elements, while react-html-parser offers similar functionality with a focus on simplicity....
🌐
CodeSandbox
codesandbox.io › s › 2y8wqoqoy
React html parser - CodeSandbox
January 29, 2020 - React html parser by rizalibnu using @material-ui/core, @material-ui/icons, react, react-dom, react-html-parser, react-scripts
Published   Dec 27, 2018
Author   rizalibnu
🌐
Npm
npm.io › package › html-react-parser
Html-react-parser NPM | npm.io
January 5, 2013 - 2 export { Parser, type ParserOptions }; ~~~~~~~~~~~~~ Then upgrade to the latest version of typescript. See #748. ... html-to-react - Single x 1,018,239 ops/sec ±0.43% (94 runs sampled) html-to-react - Multiple x 380,037 ops/sec ±0.61% (97 runs sampled) html-to-react - Complex x 35,091 ops/sec ±0.50% (96 runs sampled)
🌐
LibHunt
libhunt.com › r › html-react-parser
Html-react-parser Alternatives and Reviews (Mar 2023)
Which is the best alternative to html-react-parser? Based on common mentions it is: Next.js, React-icons, DOMPurify, Portfolio2.0, Node-csvtojson or Texthighlighter