There are now safer methods to render HTML. I covered this in a previous answer here. You have 4 options, the last uses dangerouslySetInnerHTML.

Methods for rendering HTML

  1. Easiest - Use Unicode, save the file as UTF-8 and set the charset to UTF-8.

    <div>{'First · Second'}</div>

  2. Safer - Use the Unicode number for the entity inside a Javascript string.

    <div>{'First \u00b7 Second'}</div>

    or

    <div>{'First ' + String.fromCharCode(183) + ' Second'}</div>

  3. Or a mixed array with strings and JSX elements.

    <div>{['First ', <span>&middot;</span>, ' Second']}</div>

  4. Last Resort - Insert raw HTML using dangerouslySetInnerHTML.

    <div dangerouslySetInnerHTML={{__html: 'First &middot; Second'}} />

Answer from Brett DeWoody on Stack Overflow
🌐
npm
npmjs.com › package › react-render-html
react-render-html - npm
November 10, 2017 - Latest version: 0.6.0, last published: 8 years ago. Start using react-render-html in your project by running `npm i react-render-html`. There are 84 other projects in the npm registry using react-render-html.
      » npm install react-render-html
    
Published   Nov 10, 2017
Version   0.6.0
Author   Hyunje Jun
🌐
npm
npmjs.com › package › react-native-render-html
react-native-render-html - npm
January 24, 2022 - Check out the announcement blog post in our brand new website. We also have a migration guide for those who are coming from v5 and below. ⚠️ You are on the master branch which is home for the latest development. Check the table bellow to get documentation for your exact version. ... import React from 'react'; import { useWindowDimensions } from 'react-native'; import RenderHtml from 'react-native-render-html'; const source = { html: ` <p style='text-align:center;'> Hello World!
      » npm install react-native-render-html
    
Published   Jan 24, 2022
Version   6.3.4
🌐
npm
npmjs.com › package › react-html-renderer
react-html-renderer - npm
October 25, 2021 - // src/pages/index.js import { HTML } from 'src/components' export const IndexPage = ({ html }) => <HTML html={html} /> This will render H1 elements with red text.
      » npm install react-html-renderer
    
Published   Oct 25, 2021
Version   0.3.3
Top answer
1 of 14
279

There are now safer methods to render HTML. I covered this in a previous answer here. You have 4 options, the last uses dangerouslySetInnerHTML.

Methods for rendering HTML

  1. Easiest - Use Unicode, save the file as UTF-8 and set the charset to UTF-8.

    <div>{'First · Second'}</div>

  2. Safer - Use the Unicode number for the entity inside a Javascript string.

    <div>{'First \u00b7 Second'}</div>

    or

    <div>{'First ' + String.fromCharCode(183) + ' Second'}</div>

  3. Or a mixed array with strings and JSX elements.

    <div>{['First ', <span>&middot;</span>, ' Second']}</div>

  4. Last Resort - Insert raw HTML using dangerouslySetInnerHTML.

    <div dangerouslySetInnerHTML={{__html: 'First &middot; Second'}} />

2 of 14
102

dangerouslySetInnerHTML is React’s replacement for using innerHTML in the browser DOM. In general, setting HTML from code is risky because it’s easy to inadvertently expose your users to a cross-site scripting (XSS) attack.

It is better/safer to sanitise your raw HTML (using e.g., DOMPurify) before injecting it into the DOM via dangerouslySetInnerHTML.

DOMPurify - a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG. DOMPurify works with a secure default, but offers a lot of configurability and hooks.

Example:

import React from 'react'
import createDOMPurify from 'dompurify'
import { JSDOM } from 'jsdom'

const window = (new JSDOM('')).window
const DOMPurify = createDOMPurify(window)

const rawHTML = `
<div class="dropdown">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
    Dropdown
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Another action</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Something else here</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Separated link</a></li>
  </ul>
</div>
`

const YourComponent = () => (
  <div>
    { <div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(rawHTML) }} /> }
  </div>
)

export default YourComponent
🌐
npm
npmjs.com › package › @fobos531 › react-native-render-html
@fobos531/react-native-render-html - npm
import React from 'react'; import { useWindowDimensions } from 'react-native'; import RenderHtml from '@fobos531/react-native-render-html'; const source = { html: ` <p style='text-align:center;'> Hello World!
      » npm install @fobos531/react-native-render-html
    
Published   Jul 04, 2024
Version   6.4.1
🌐
GitHub
github.com › meliorence › react-native-render-html
GitHub - meliorence/react-native-render-html: iOS/Android pure javascript react-native component that renders your HTML into 100% native views · GitHub
npm install react-native-render-html · yarn add react-native-render-html · import React from 'react'; import { useWindowDimensions } from 'react-native'; import RenderHtml from 'react-native-render-html'; const source = { html: ` <p ...
Starred by 3.6K users
Forked by 626 users
Languages   TypeScript 76.5% | MDX 11.1% | JavaScript 8.2% | SCSS 3.4%
🌐
W3Schools
w3schools.com › react › react_render.asp
React Render HTML
React uses a container to render HTML in a web page.
🌐
GitHub
github.com › hatashiro › react-render-html
GitHub - hatashiro/react-render-html: Render HTML as React element, possibly replacing dangerouslySetInnerHTML · GitHub
May 4, 2020 - import renderHTML from 'react-render-html'; renderHTML("<a class='github' href='https://github.com'><b>GitHub</b></a>") // => React Element // <a className="github" href="https://github.com"><b>GitHub</b></a> It may be used in the render method in a React component:
Starred by 214 users
Forked by 30 users
Languages   JavaScript
Find elsewhere
Top answer
1 of 14
279

There are now safer methods to render HTML. I covered this in a previous answer here. You have 4 options, the last uses dangerouslySetInnerHTML.

Methods for rendering HTML

  1. Easiest - Use Unicode, save the file as UTF-8 and set the charset to UTF-8.

    <div>{'First · Second'}</div>

  2. Safer - Use the Unicode number for the entity inside a Javascript string.

    <div>{'First \u00b7 Second'}</div>

    or

    <div>{'First ' + String.fromCharCode(183) + ' Second'}</div>

  3. Or a mixed array with strings and JSX elements.

    <div>{['First ', <span>&middot;</span>, ' Second']}</div>

  4. Last Resort - Insert raw HTML using dangerouslySetInnerHTML.

    <div dangerouslySetInnerHTML={{__html: 'First &middot; Second'}} />

2 of 14
102

dangerouslySetInnerHTML is React’s replacement for using innerHTML in the browser DOM. In general, setting HTML from code is risky because it’s easy to inadvertently expose your users to a cross-site scripting (XSS) attack.

It is better/safer to sanitise your raw HTML (using e.g., DOMPurify) before injecting it into the DOM via dangerouslySetInnerHTML.

DOMPurify - a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG. DOMPurify works with a secure default, but offers a lot of configurability and hooks.

Example:

import React from 'react'
import createDOMPurify from 'dompurify'
import { JSDOM } from 'jsdom'

const window = (new JSDOM('')).window
const DOMPurify = createDOMPurify(window)

const rawHTML = `
<div class="dropdown">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
    Dropdown
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Another action</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Something else here</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Separated link</a></li>
  </ul>
</div>
`

const YourComponent = () => (
  <div>
    { <div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(rawHTML) }} /> }
  </div>
)

export default YourComponent
🌐
KindaCode
kindacode.com › article › ways-to-render-html-content-in-react-and-jsx
2 Ways to Render HTML Content in React and JSX - KindaCode
If you have a strong reason not to use dangerouslySetInnerHTML, you can use a third-party library that doesn’t use dangerouslySetInnerHTML internally. There are several npm packages that meet this requirement, such as html-to-react or react-render-html.
🌐
FlatCoding
flatcoding.com › home › how to render html elements in react.js
How to Render HTML Elements in React.js - FlatCoding
March 30, 2025 - The general purpose of this attribute is that you can save raw HTML in a database, and then, when you retrieve it, it can be rendered using the dangerouslySetInnerHTML attribute. ... import React from 'react'; import ReactDOM from 'react-dom'; class CodedTagComponent extends React.Component ...
🌐
npm
npmjs.com › package › react-html-element
react-html-element - npm
The key pieces of code are ... extends ReactHTMLElement and this.render, which mounts our app to its designated mountPoint, as described below. One thing to remember is that you will need to load the webcomponentsjs polyfills for ReactHTMLElement to work in all browsers. <script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/2.4.3/webcomponents-bundle.js"></script> There are many ways to implement these polyfills, and you can explore them in the webpcomponentsjs README.
      » npm install react-html-element
    
Published   Mar 05, 2025
Version   4.0.5
Author   Steve Matney
🌐
npm
npmjs.com › search
render html - npm search
No more dangerouslySetInnerHTML, render HTML as React element.
🌐
npm
npmjs.com › package › @vandrei977 › react-native-render-html
@vandrei977/react-native-render-html - npm
Start using @vandrei977/react-native-render-html in your project by running `npm i @vandrei977/react-native-render-html`. There are no other projects in the npm registry using @vandrei977/react-native-render-html.
      » npm install @vandrei977/react-native-render-html
    
🌐
npm
npmjs.com › package › react-html-render
react-html-render - npm
June 2, 2016 - Latest version: 1.0.3, last published: 9 years ago. Start using react-html-render in your project by running `npm i react-html-render`. There are no other projects in the npm registry using react-html-render.
      » npm install react-html-render
    
Published   Jun 02, 2016
Version   1.0.3
Author   HsuTing
Top answer
1 of 6
72

If your template.html file is just HTML and not a React component, then you can't require it in the same way you would do with a JS file.

However, if you are using Browserify — there is a transform called stringify which will allow you to require non-js files as strings. Once you have added the transform, you will be able to require HTML files and they will export as though they were just strings.

Once you have required the HTML file, you'll have to inject the HTML string into your component, using the dangerouslySetInnerHTML prop.

var __html = require('./template.html');
var template = { __html: __html };

React.module.exports = React.createClass({
  render: function() {
    return(
      <div dangerouslySetInnerHTML={template} />
    );
  }
});

This goes against a lot of what React is about though. It would be more natural to create your templates as React components with JSX, rather than as regular HTML files.

The JSX syntax makes it trivially easy to express structured data, like HTML, especially when you use stateless function components.

If your template.html file looked something like this

<div class='foo'>
  <h1>Hello</h1>
  <p>Some paragraph text</p>
  <button>Click</button>
</div>

Then you could convert it instead to a JSX file that looked like this.

module.exports = function(props) {
  return (
    <div className='foo'>
      <h1>Hello</h1>
      <p>Some paragraph text</p>
      <button>Click</button>
    </div>
  );
};

Then you can require and use it without needing stringify.

var Template = require('./template');

module.exports = React.createClass({
  render: function() {
    var bar = 'baz';
    return(
      <Template foo={bar}/>
    );
  }
});

It maintains all of the structure of the original file, but leverages the flexibility of React's props model and allows for compile time syntax checking, unlike a regular HTML file.

2 of 6
8

You can use the dangerouslySetInnerHTML property to inject arbitrary HTML:

// Assume from another require()'ed module:
var html = '<h1>Hello, world!</h1>'

var MyComponent = React.createClass({
  render: function() {
    return React.createElement("h1", {dangerouslySetInnerHTML: {__html: html}})
  }
})

ReactDOM.render(React.createElement(MyComponent), document.getElementById('app'))
<script src="https://fb.me/react-0.14.3.min.js"></script>
<script src="https://fb.me/react-dom-0.14.3.min.js"></script>
<div id="app"></div>

You could even componentize this template behavior (untested):

class TemplateComponent extends React.Component {
  constructor(props) {
    super(props)
    this.html = require(props.template)
  }

  render() {
    return <div dangerouslySetInnerHTML={{__html: this.html}}/>
  }

}

TemplateComponent.propTypes = {
  template: React.PropTypes.string.isRequired
}

// use like
<TemplateComponent template='./template.html'/>

And with this, template.html (in the same directory) looks something like (again, untested):

// ./template.html
module.exports = '<h1>Hello, world!</h1>'
🌐
Npm
npm.io › package › react-render-html
React-render-html NPM | npm.io
import renderHTML from 'react-render-html'; renderHTML("<a class='github' href='https://github.com'><b>GitHub</b></a>") // => React Element // <a className="github" href="https://github.com"><b>GitHub</b></a> It may be used in the render method in a React component: