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
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
🌐
Reddit
reddit.com › r/frontend › how do i render react to raw html / css without any javascript
How do I render React to raw HTML / CSS without any ...
September 27, 2020 -

I am using material-ui and react for my project. I would like to create some HTML email templates using the same style and design like my website.

I was thinking about mocking up the HTML page in react using React and material-ui components. Once I do, will I be able to render this to raw HTML? I don't want to email heavy javascript files, so ideally it would be a light as possible.

Can this be done? Or am I barking up the wrong tree?

Discussions

React rendering raw HTML code instead of interpreting it
I have a function that returns a string of a number of divs equal to the number passed to it. When I call the function in my component it just shows the string of HTML code instead of interpreting it. I tried writing a div outside of the curly braces to see how it would evaluate. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
4
0
July 14, 2018
Insert HTML with React Variable Statements (JSX)
I am building something with React where I need to insert HTML with React Variables in JSX. Is there a way to have a variable like so: var thisIsMyCopy = ' copy copy copy str... More on stackoverflow.com
🌐 stackoverflow.com
React Components vs Pure HTML
It's up to you, React doesn't have a strong opinion. Back when React got started, it was touted as a framework that could be included for a small portion of your app, capable of scaling up as needed. You could probably even go wild and have React, Angular, and Vue all together in a single app. Personally though, I let React handle almost everything (occasionally handing things off to D3 or THREE to do their magic). More on reddit.com
🌐 r/reactjs
8
3
March 29, 2021
How do I render React to raw HTML / CSS without any Javascript
You should be aware that email clients are a different world than web browsers. The majority of emails are still developed using tables for layout, as support for various CSS properties is mixed. For example, Outlook on Windows doesn't support flexbox at all. Your material UI components may look completely broken in some email clients. https://www.caniemail.com/scoreboard/ More on reddit.com
🌐 r/Frontend
8
0
September 27, 2020
🌐
Docureacten
docureacten.github.io › safely using raw html
Safely Using Raw HTML | React.js: Learn Easily with Examples
Injecting raw HTML into React components using dangerouslySetInnerHTML can be necessary but also poses significant security risks. To mitigate these risks, always sanitize the HTML before rendering it and avoid using this method with dynamic ...
🌐
W3Schools
w3schools.com › react › react_render.asp
React Render HTML
The root node is the HTML element where you want to display the result. It is like a container for content, managed by React.
🌐
Medium
medium.com › course-hero-engineering › dynamic-react-components-in-raw-html-3fa6991ba352
Dynamic React Components in Raw HTML | by Michael Kramer | Course Hero Engineering | Medium
August 15, 2022 - We created the HTMLToComponentPipeline to take in raw HTML and spit out a React DOM tree with a mix of standard HTML React nodes (<div>,<span>, etc.) and our React components. It supports SSR and client-side hydration.
🌐
DhiWise
dhiwise.com › post › react-inject-html-a-comprehensive-guide
The Ultimate Guide to React HTML Injection
September 5, 2024 - Injecting HTML into React components can be straightforward, but it must be done securely to prevent cross site scripting attacks. React provides a property called dangerouslySetInnerHTML to set raw HTML directly from a string.
Find elsewhere
🌐
npm
npmjs.com › package › react-raw-html
react-raw-html - npm
March 30, 2016 - React components that output their children as raw HTML.. Latest version: 0.4.0, last published: 10 years ago. Start using react-raw-html in your project by running `npm i react-raw-html`. There are 1 other projects in the npm registry using react-raw-html.
      » npm install react-raw-html
    
Published   Mar 30, 2016
Version   0.4.0
Author   Sasha Aickin
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
React rendering raw HTML code instead of interpreting it
July 14, 2018 - I have a function that returns a string of a number of divs equal to the number passed to it. When I call the function in my component it just shows the string of HTML code instead of interpreting it. I tried writing a d…
🌐
DEV Community
dev.to › jobpick › how-to-render-html-string-in-a-react-component-3kd2
How to Render HTML string in a React component ? - DEV Community
August 3, 2021 - Most of the rich text editor for react uses html for the formatting. So the data in the text editor is saved as string. ... We cannot directly render this string, html tags will also get treated as raw string.
🌐
Pluralsight
pluralsight.com › what makes pluralsight different | pluralsight › tech guides & tutorials
How to Use Static HTML with React | Pluralsight
July 3, 2020 - In this guide, you will learn how you can embed raw HTML inside a component. If you try to render an HTML string inside a component directly, React will automatically sanitize it and render it as a plain string.
🌐
npm
npmjs.com › package › raw-html-react
raw-html-react - npm
August 6, 2018 - A React component that converts raw HTML to React components.
      » npm install raw-html-react
    
Published   Aug 06, 2018
Version   1.2.3
🌐
NashTech Blog
blog.nashtechglobal.com › home › render the raw html string in reactjs
render html string in reactjs
November 8, 2024 - So, sanitize HTML before using. setHtml(sanitizeHtml(rawHTML)); }, [html]); return( <div dangerouslySetInnerHTML={{__html: html}}></div> ) } The ReactDOMServer module is a part of the official React library. It provides methods for rendering React components to static HTML.
🌐
CodeSandbox
codesandbox.io › s › rendering-raw-html-data-in-react-js-3slkv
Rendering raw html data in react js - CodeSandbox
April 6, 2020 - Rendering raw html data in react js by adi501 using html-react-parser, react, react-dom, react-scripts
Published   Apr 06, 2020
Author   adi501
🌐
Kieran Barker
barker.codes › blog › safely-rendering-html-in-react
Safely rendering HTML in React - Kieran Barker
September 19, 2022 - We need to use the dangerouslySetInnerHTML prop to render the strings as HTML instead of plain text: 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.
🌐
Medium
remelehane.medium.com › rendering-html-in-your-react-native-application-ca9d1585508e
Rendering HTML in your React Native application | by Reme Le Hane | Medium
October 28, 2018 - Currently the best option available within react-native itself is the WebView, however I found this to be a component to be very similar to the web iFrame, which means that certain things that we would like to be simple are not quite. While it is pretty simply to place the raw HTML into the WebView, it does need to be manually or programmatically sized, not something natively taken care of by the component.
🌐
CodeSandbox
codesandbox.io › examples › package › react-raw-html
react-raw-html examples - CodeSandbox
Use this online react-raw-html playground to view and fork react-raw-html example apps and templates on CodeSandbox.
🌐
npm Trends
npmtrends.com › html-to-react-vs-raw-html-react-vs-react-render-html
html-to-react vs raw-html-react vs react-render-html | npm trends
Comparing trends for html-to-react 1.7.0 which has 407,129 weekly downloads and 791 GitHub stars vs. raw-html-react 1.2.3 which has 24 weekly downloads and 7 GitHub stars vs. react-render-html 0.6.0 which has 19,933 weekly downloads and 216 GitHub stars.