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 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.
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
npmjs.com › package › html-react-parser
html-react-parser - npm
1 month ago - HTML to React parser.. Latest version: 6.0.1, last published: 24 days ago. Start using html-react-parser in your project by running `npm i html-react-parser`. There are 1801 other projects in the npm registry using html-react-parser.
      » npm install html-react-parser
    
Published   Apr 08, 2026
Version   6.0.1
🌐
Reddit
reddit.com › r/reactjs › alternatives to dangerouslysetinnerhtml
r/reactjs on Reddit: Alternatives to dangerouslySetInnerHTML
March 7, 2023 -

I have to render some small snippets of HTML in a control. The source is trusted and the app is internal but i still cannot take any XSS chances.

I have to render html from an email body bc apparently HTML is hard for digital marketing people to read haha..

I was thinking about some sanitization library but found this: https://github.com/remarkablemark/html-react-parser which i havent reviewed to deeply yet.

Anybody been down this road and can help a brother out with a recommendation?

🌐
Medium
remarkablemark.medium.com › an-alternative-to-dangerously-set-innerhtml-64a12a7c1f96
An alternative to dangerously set innerHTML - remarkablemark - Medium
May 4, 2024 - An alternative to dangerously set innerHTML You can use html-react-parser instead. There may be an occasion that you need to render an HTML string when using React. In most situations …
Find elsewhere
🌐
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.
🌐
npm Trends
npmtrends.com › cheerio-vs-html-react-parser-vs-htmlparser-vs-htmlparser2-vs-react-html-parser-vs-react-render-html-vs-react-sanitized-html-vs-sanitize-html-vs-sanitize-html-react
cheerio vs html-react-parser vs htmlparser vs htmlparser2 vs react-html-parser vs react-render-html vs react-sanitized-html vs sanitize-html vs sanitize-html-react | npm trends
Comparing trends for cheerio 1.1.2 which has 12,316,525 weekly downloads and 29,825 GitHub stars vs. html-react-parser 5.2.7 which has 2,114,534 weekly downloads and 2,349 GitHub stars vs. htmlparser 1.7.7 which has 98,557 weekly downloads and 1,146 GitHub stars vs. htmlparser2 10.0.0 which has 43,146,190 weekly downloads and 4,652 GitHub stars vs. react-html-parser 2.0.2 which has 246,688 weekly downloads and 797 GitHub stars vs. react-render-html 0.6.0 which has 22,618 weekly downloads and 216 GitHub stars vs. react-sanitized-html 2.0.0 which has 6,965 weekly downloads and 20 GitHub stars vs. sanitize-html 2.17.0 which has 3,609,741 weekly downloads and 4,073 GitHub stars vs. sanitize-html-react 1.13.0 which has 25,285 weekly downloads and 27 GitHub stars.
🌐
npm Trends
npmtrends.com › html-react-parser-vs-react-html-parser-vs-react-jsx-parser-vs-react-render-html-vs-react-sanitized-html
html-react-parser vs react-html-parser vs react-jsx-parser vs react-render-html vs react-sanitized-html | npm trends
Comparing trends for html-react-parser 5.2.8 which has 2,210,001 weekly downloads and 2,364 GitHub stars vs. react-html-parser 2.0.2 which has 228,728 weekly downloads and 797 GitHub stars vs. react-jsx-parser 2.4.1 which has 77,335 weekly downloads and 720 GitHub stars vs. react-render-html 0.6.0 which has 21,871 weekly downloads and 214 GitHub stars vs. react-sanitized-html 2.0.0 which has 6,111 weekly downloads and 20 GitHub stars.
🌐
npm Trends
npmtrends.com › html-react-parser-vs-interweave-vs-react-html-parser-vs-react-htmlparser2
html-react-parser vs interweave vs react-html-parser vs react-htmlparser2 | npm trends
Comparing trends for html-react-parser 5.2.16 which has 2,672,760 weekly downloads and 2,384 GitHub stars vs. interweave 13.1.1 which has 115,889 weekly downloads and 1,151 GitHub stars vs. react-html-parser 2.0.2 which has 280,383 weekly downloads and 797 GitHub stars vs. react-htmlparser2 ...
🌐
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 139 users
Languages   TypeScript 87.9% | JavaScript 12.1%
🌐
vimtutor
remarkablemark.org › blog › 2016 › 10 › 07 › dangerously-set-innerhtml-alternative
Dangerously set innerHTML alternative | remarkablemark
October 7, 2016 - An alternative to React’s dangerouslySetInnerHTML is html-react-parser, which converts an HTML string to React elements.
🌐
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)
🌐
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
🌐
React.js Examples
reactjsexample.com › a-html-to-react-parser
A HTML to React parser
July 3, 2019 - $ npm install html-react-parser --save · Yarn: $ yarn add html-react-parser ·
🌐
GitHub
github.com › peternewnham › react-html-parser
GitHub - peternewnham/react-html-parser: Converts HTML strings directly into React components and provide a simple way to modify and replace the content. · GitHub
Converts standard HTML elements, attributes and inline styles into their React equivalents and provides a simple way to modify and replace the content.
Starred by 797 users
Forked by 103 users
Languages   JavaScript
🌐
CodeSandbox
codesandbox.io › examples › package › react-html-parser
react-html-parser examples - CodeSandbox
Use this online react-html-parser playground to view and fork react-html-parser example apps and templates on CodeSandbox.
🌐
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
🌐
LibHunt
libhunt.com › r › react-html-parser
React-html-parser Alternatives and Reviews (Apr 2021)
April 2, 2021 - Which is the best alternative to react-html-parser? Based on common mentions it is: Styled-components, Headlessui, Xstate, React-gallery-carousel or Downshift 🏎