You can consider using the React attribute dangerouslySetInnerHTML:
class YourComponent{
render() {
someHtml = '<div><strong>blablabla<strong><p>another blbla</p/></div>'
return (
<div className="Container" dangerouslySetInnerHTML={{__html: someHtml}}></div>
)
}
}
Answer from user8296051 on Stack Overflowreactjs - How do I convert a string to jsx? - Stack Overflow
want to convert text to react compnent
Saving Script file to JSX - Adobe Photoshop - Creative COW
convert an html file into jsx file
Why choose this HTML to JSX converter?
Can I export the converted JSX code?
How do I convert TypeScript to JavaScript?
You can consider using the React attribute dangerouslySetInnerHTML:
class YourComponent{
render() {
someHtml = '<div><strong>blablabla<strong><p>another blbla</p/></div>'
return (
<div className="Container" dangerouslySetInnerHTML={{__html: someHtml}}></div>
)
}
}
2023 answer update
Seem react-html-parser has no longer been maintained for a few years already. As pointed on the comment by Bill Keller, there is a library still actively maintained named html-react-parser. However, please read carefully on the Readme and make some considerations before using it in production. Like in this section Is this XSS safe?, if it is acceptable, then you are good to go.
Another alternative sounds more promising to me because it emphasizes "Safely" on their repo and home page. The library is named "Interweave". Please check the following repo for more detail about installation and usage: https://github.com/milesj/interweave/
2018 answer
Personally, I love to do it just like in the previous answer which recommends the usage of dangerouslySetInnerHTML property in JSX.
Just for an alternative, nowadays there is a library called react-html-parser. You can check it and install from NPM registry at this URL: https://www.npmjs.com/package/react-html-parser. Today's weekly download statistic for that package is 23,696. Looks a quite popular library to use. Even it looks more convenient to use, my self, still need more read and further consideration before really using it.
Code snippet copied from the NPM page:
import React from 'react';
import ReactHtmlParser, { processNodes, convertNodeToElement, htmlparser2 } from 'react-html-parser';
class HtmlComponent extends React.Component {
render() {
const html = '<div>Example HTML string</div>';
return <div>{ ReactHtmlParser(html) }</div>;
}
}
So i have a text, i need to convert it into a react component and render it ,
const [jsxCode, setJsxCode] = useState(\
<mesh><boxGeometry args={[1, 1, 1]} /><meshBasicMaterial color="red" /></mesh>`);`
I tried different things
-
using dangerouslySetInnerHTML inside the canvas, but that is not being allowed as only children or this is allowed
-
tried using a parser library not working
-
React.createElement(React.Fragment, {}, jsxCode);, this will have worked if jsxCode was not a string , but actual components
const [jsxCode, setJsxCode] = useState(<mesh><boxGeometry args={[1, 1, 1]} /><meshBasicMaterial color="red" /></mesh>);
Any pointers is deeply appreciated on conversion
))} ) }
» npm install @madebyconnor/rich-text-to-jsx