Is this.props.match.description a string or an object? If it's a string, it should be converted to HTML just fine. Example:

class App extends React.Component {

constructor() {
    super();
    this.state = {
      description: '<h1 style="color:red;">something</h1>'
    }
  }
  
  render() {
    return (
      <div dangerouslySetInnerHTML={{ __html: this.state.description }} />
    );
  }
}

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

Result: http://codepen.io/ilanus/pen/QKgoLA?editors=1011

However if description is <h1 style="color:red;">something</h1> without the quotes '', you're going to get:

​Object {
$$typeof: [object Symbol] {},
  _owner: null,
  key: null,
  props: Object {
    children: "something",
    style: "color:red;"
  },
  ref: null,
  type: "h1"
}

If It's a string and you don't see any HTML markup the only problem I see is wrong markup..

UPDATE

If you are dealing with HTML Entities, You need to decode them before sending them to dangerouslySetInnerHTML that's why it's called "dangerously" :)

Working example:

class App extends React.Component {

  constructor() {
    super();
    this.state = {
      description: '&lt;p&gt;&lt;strong&gt;Our Opportunity:&lt;/strong&gt;&lt;/p&gt;'
    }
  }

   htmlDecode(input){
    var e = document.createElement('div');
    e.innerHTML = input;
    return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
  }

  render() {
    return (
      <div dangerouslySetInnerHTML={{ __html: this.htmlDecode(this.state.description) }} />
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
Answer from Ilanus on Stack Overflow
Top answer
1 of 16
290

Is this.props.match.description a string or an object? If it's a string, it should be converted to HTML just fine. Example:

class App extends React.Component {

constructor() {
    super();
    this.state = {
      description: '<h1 style="color:red;">something</h1>'
    }
  }
  
  render() {
    return (
      <div dangerouslySetInnerHTML={{ __html: this.state.description }} />
    );
  }
}

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

Result: http://codepen.io/ilanus/pen/QKgoLA?editors=1011

However if description is <h1 style="color:red;">something</h1> without the quotes '', you're going to get:

​Object {
$$typeof: [object Symbol] {},
  _owner: null,
  key: null,
  props: Object {
    children: "something",
    style: "color:red;"
  },
  ref: null,
  type: "h1"
}

If It's a string and you don't see any HTML markup the only problem I see is wrong markup..

UPDATE

If you are dealing with HTML Entities, You need to decode them before sending them to dangerouslySetInnerHTML that's why it's called "dangerously" :)

Working example:

class App extends React.Component {

  constructor() {
    super();
    this.state = {
      description: '&lt;p&gt;&lt;strong&gt;Our Opportunity:&lt;/strong&gt;&lt;/p&gt;'
    }
  }

   htmlDecode(input){
    var e = document.createElement('div');
    e.innerHTML = input;
    return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
  }

  render() {
    return (
      <div dangerouslySetInnerHTML={{ __html: this.htmlDecode(this.state.description) }} />
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
2 of 16
160

I use 'react-html-parser'

yarn add react-html-parser
import ReactHtmlParser from 'react-html-parser'; 

<div> { ReactHtmlParser (html_string) } </div>

Source on npmjs.com

Lifting up @okram's comment for more visibility:

from its github description: Converts HTML strings directly into React components avoiding the need to use dangerouslySetInnerHTML from npmjs.com 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.

Top answer
1 of 16
290

Is this.props.match.description a string or an object? If it's a string, it should be converted to HTML just fine. Example:

class App extends React.Component {

constructor() {
    super();
    this.state = {
      description: '<h1 style="color:red;">something</h1>'
    }
  }
  
  render() {
    return (
      <div dangerouslySetInnerHTML={{ __html: this.state.description }} />
    );
  }
}

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

Result: http://codepen.io/ilanus/pen/QKgoLA?editors=1011

However if description is <h1 style="color:red;">something</h1> without the quotes '', you're going to get:

​Object {
$$typeof: [object Symbol] {},
  _owner: null,
  key: null,
  props: Object {
    children: "something",
    style: "color:red;"
  },
  ref: null,
  type: "h1"
}

If It's a string and you don't see any HTML markup the only problem I see is wrong markup..

UPDATE

If you are dealing with HTML Entities, You need to decode them before sending them to dangerouslySetInnerHTML that's why it's called "dangerously" :)

Working example:

class App extends React.Component {

  constructor() {
    super();
    this.state = {
      description: '&lt;p&gt;&lt;strong&gt;Our Opportunity:&lt;/strong&gt;&lt;/p&gt;'
    }
  }

   htmlDecode(input){
    var e = document.createElement('div');
    e.innerHTML = input;
    return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
  }

  render() {
    return (
      <div dangerouslySetInnerHTML={{ __html: this.htmlDecode(this.state.description) }} />
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
2 of 16
160

I use 'react-html-parser'

yarn add react-html-parser
import ReactHtmlParser from 'react-html-parser'; 

<div> { ReactHtmlParser (html_string) } </div>

Source on npmjs.com

Lifting up @okram's comment for more visibility:

from its github description: Converts HTML strings directly into React components avoiding the need to use dangerouslySetInnerHTML from npmjs.com 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.

Discussions

How can I turn react component in to html string
https://react.dev/reference/react-dom/server/renderToString More on reddit.com
🌐 r/reactjs
21
0
January 7, 2024
How to render a string from MongoDB with HTML tags in JSX?
Maybe use some type of Markdown renderer More on reddit.com
🌐 r/reactjs
18
7
December 31, 2024
Top answer
1 of 13
231

Is this.props.match.description a string or an object? If it's a string, it should be converted to HTML just fine. Example:

class App extends React.Component {

constructor() {
    super();
    this.state = {
      description: '<h1 style="color:red;">something</h1>'
    }
  }
  
  render() {
    return (
      <div dangerouslySetInnerHTML={{ __html: this.state.description }} />
    );
  }
}

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

Result: http://codepen.io/ilanus/pen/QKgoLA?editors=1011

However if description is <h1 style="color:red;">something</h1> without the quotes '', you're going to get:

​Object {
$$typeof: [object Symbol] {},
  _owner: null,
  key: null,
  props: Object {
    children: "something",
    style: "color:red;"
  },
  ref: null,
  type: "h1"
}

If It's a string and you don't see any HTML markup the only problem I see is wrong markup..

UPDATE

If you are dealing with HTML Entities, You need to decode them before sending them to dangerouslySetInnerHTML that's why it's called "dangerously" :)

Working example:

class App extends React.Component {

  constructor() {
    super();
    this.state = {
      description: '&lt;p&gt;&lt;strong&gt;Our Opportunity:&lt;/strong&gt;&lt;/p&gt;'
    }
  }

   htmlDecode(input){
    var e = document.createElement('div');
    e.innerHTML = input;
    return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
  }

  render() {
    return (
      <div dangerouslySetInnerHTML={{ __html: this.htmlDecode(this.state.description) }} />
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
2 of 13
127

I use 'react-html-parser'

yarn add react-html-parser
import ReactHtmlParser from 'react-html-parser'; 

<div> { ReactHtmlParser (html_string) } </div>

Source on npmjs.com

Lifting up @okram's comment for more visibility:

from its github description: Converts HTML strings directly into React components avoiding the need to use dangerouslySetInnerHTML from npmjs.com 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.

🌐
Reactgo
reactgo.com › home › rendering the html string in react
Rendering the HTML string in React | Reactgo
January 28, 2023 - ... import React from "react"; export default function App() { const htmlString = "<h1>Hello World</h1>"; return <div>{htmlString}</div>; } To render the html string in react, we can use the dangerouslySetInnerHTML attribute which is a react ...
🌐
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 - The easiest solution for this is to use dangerouslySetInnerHTML. <div dangerouslySetInnerHTML={{ __html: "<p>some data </p>" }} /> by using dangerouslySetInnerHTML, entire html in the string is preserved.
🌐
YouTube
youtube.com › watch
Render HTML string as real HTML in a React component - YouTube
Forgot to mention in the video that I got the idea from this stackoverflow page: https://stackoverflow.com/questions/39758136/render-html-string-as-real-html...
Published   March 10, 2020
🌐
NashTech Blog
blog.nashtechglobal.com › home › render the raw html string in reactjs
render html string in reactjs
November 8, 2024 - import React from "react"; import ReactDOMServer from "react-dom/server"; const reactString = "<div>Hello, world!</div>"; const htmlString = ReactDOMServer.renderToString(React.createElement("div", { dangerouslySetInnerHTML: { __html: reactString } })); console.log(htmlString); // Output: <div>Hello, world!</div> Same with client rendering, we also should sanitize HTML string before that.
Find elsewhere
🌐
GitHub
github.com › hatashiro › react-render-html
GitHub - hatashiro/react-render-html: Render HTML as React element, possibly replacing dangerouslySetInnerHTML · GitHub
May 4, 2020 - It renders a provided HTML string into a React element. 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
🌐
React
react.dev › reference › react-dom › server › renderToString
renderToString – React
If some component suspends (for example, because it’s defined with lazy or fetches data), renderToString will not wait for its content to resolve. Instead, renderToString will find the closest <Suspense> boundary above it and render its fallback prop in the HTML.
🌐
Emmanuel Gautier
emmanuelgautier.com › home › blog › how to render a react element to an html string?
How to render a React element to an HTML string?
February 15, 2023 - To convert a React string to an HTML string, we need to use the renderToString method provided by ReactDOMServer. The renderToString method takes a React component as an argument and returns a string of HTML.
🌐
DEV Community
dev.to › diwakarkashyap › convert-html-string-to-html-in-react-all-advance-methods-38hg
convert HTML string to HTML in React (all advance methods) - DEV Community
August 13, 2023 - If the HTML content has associated styles and you want to scope them to avoid conflicts, you can use libraries like styled-components or emotion to apply styles only to that chunk of content. ... You can render the content inside a shadow root which provides encapsulation for JavaScript, CSS, and templates. This can be done using React refs and direct DOM manipulation but is more involved.
🌐
Medium
medium.com › @diwakarkashyap › convert-html-string-to-html-in-react-all-advance-methods-8953590d8f39
convert HTML string to HTML in React (all advance methods | by Diwakar Kashyap | Medium
August 13, 2023 - convert HTML string to HTML in React (all advance methods If you need an advanced way to convert an HTML string to actual rendered content in React, you might be looking at incorporating richer …
🌐
Reddit
reddit.com › r/reactjs › how can i turn react component in to html string
r/reactjs on Reddit: How can I turn react component in to html string
January 7, 2024 -

How can I turn a SPA react component with its styling in to an html template? I want to store this template on my backend for injecting data and creating pdfs. Client side pdf generation is not an option. Neither is visiting the site using a headless browser tool like Puppeteer.
I thought this would be easier, I thought react might offer some one-click option for exporting a component, but it is proving to be tricky.

  • I have tried renderToString with some success. It provides me with the correct html tree. It still doesn’t help with gathering styles easily.

  • I have tried to use chrome dev tools to copy html element but this issues comes with styling. chrome dev tools does not seem to offer a way to easily extract styles

  • I have tried some chrome extensions like css-used or snappysnippet but the css was always off a significant amount

The best solution I have found to get an html string with styling closely mirroring the react component is manually copying from chrome dev tools. This isn't a maintainable solution though. I was looking for a 1 to1 React component -> html string solution.
Does anyone have any ideas?

🌐
Medium
medium.com › @uigalaxy7 › how-to-render-html-in-react-7f3c73f5cafc
How to render HTML string in React | by UI Galaxy | Medium
December 18, 2019 - As an alternative to dangerouslySetInnerHTML you can use html-react-parser library. var parse = require('html-react-parser');parse('<div>text</div>'); It converts an HTML string to one or more React elements.
🌐
npm
npmjs.com › package › react-render-html
react-render-html - npm
November 10, 2017 - It renders a provided HTML string into a React element.
      » npm install react-render-html
    
Published   Nov 10, 2017
Version   0.6.0
Author   Hyunje Jun
🌐
Delft Stack
delftstack.com › home › howto › react › react render html string
How to Render HTML String in React | Delft Stack
February 2, 2024 - Then, let’s render HTML string to root. # react class App extends React.Component { constructor() { super(); this.state = { TextRender: '<h1>This is a heading</h1><p> This is a paragraph.</p>' } } render() { return ( <div dangerouslySetInnerHTML={{ __html: this.state.TextRender }} /> ); } } ReactDOM.render(<App />, document.getElementById('root'));
🌐
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.
🌐
Educative
educative.io › answers › how-to-render-html-in-a-state-string
How to render HTML in a state string
The react-html-parser library converts an HTML string to a React element. Run the following command to install the library using npm: ... We render the HTML content in the state of the App component.
🌐
GitHub
github.com › MomenSherif › react-html-string
GitHub - MomenSherif/react-html-string: Convert HTML strings into React components 🚀
Avoids the use of dangerouslySetInnerHTML and converts standard HTML elements, attributes and inline styles into their React equivalents or Custom Components. $ npm install react-html-string or $ yarn add react-html-string · import HTMLString from 'react-html-string'; const html = ` <div> <h1> Hello from <a href="https://github.com/MomenSherif/react-html-string" target="_blank" >React HTML String</a > </h1> <hr /> <ul> <li>Render HTML string safely</li> <li>Provide <b>Custom Components</b> if needed</li> </ul> <p>Don't forget to ⭐️ the project</p> </div> `; export default function App() { return ( <div> <h1>Hello, React!</h1> <HTMLString html={html} /> </div> ); }
Author   MomenSherif