By default, React escapes the HTML to prevent XSS (Cross-site scripting). If you really want to render HTML, you can use the dangerouslySetInnerHTML property:
<td dangerouslySetInnerHTML={{__html: this.state.actions}} />
React forces this intentionally-cumbersome syntax so that you don't accidentally render text as HTML and introduce XSS bugs.
Answer from Sophie Alpert on Stack OverflowBy default, React escapes the HTML to prevent XSS (Cross-site scripting). If you really want to render HTML, you can use the dangerouslySetInnerHTML property:
<td dangerouslySetInnerHTML={{__html: this.state.actions}} />
React forces this intentionally-cumbersome syntax so that you don't accidentally render text as HTML and introduce XSS bugs.
There are now safer methods to accomplish this. The docs have been updated with these methods.
Other Methods
Easiest - Use Unicode, save the file as UTF-8 and set the
charsetto UTF-8.<div>{'First · Second'}</div>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>`
Or a mixed array with strings and JSX elements.
<div>{['First ', <span>·</span>, ' Second']}</div>Last Resort - Insert raw HTML using
dangerouslySetInnerHTML.<div dangerouslySetInnerHTML={{__html: 'First · Second'}} />
want to convert text to react compnent
javascript - Convert text to HTML - Stack Overflow
javascript - How to convert html string into plain text in React? - Stack Overflow
javascript - React plain text to html code - Stack Overflow
Videos
» npm install html-react-parser
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 html-converter-react
As said above you can use DOMParser
const parser = new DOMParser(),
dom = parser.parseFromString(text, "text/html");
then access the dom with habitual methods
let myDivContent = dom.querySelector('.myDiv').innerHTML;
You can do something like this, its just plain old Vanila JS.
//This is your fetched html string
let stringHTML = "<div> <h2>Some data</h2><div class='myDiv'> <h1> Test </h1> </div> </div>";
//Create temp element holder
let tempElement = document.createElement("div");
//Add fetched HTML to tempElement
tempElement.innerHTML = stringHTML;
//Now you can query it as it was a normal DOM element
let elementsWithClass = tempElement.getElementsByClassName("myDiv")
document.getElementById('root').appendChild(elementsWithClass[0]);
<div id='root'>
</div>
» npm install react-html-parser
I figured it out. I need to put it inside a div with a special attribute:
<div dangerouslySetInnerHTML={{ __html: item.snippet }} />
LIVE EXAMPLE
class App extends React.Component {
constructor() {
super();
this.state = {
snippet: `Chainsmokers were re-formed as an EDM DJ duo in 2012 under the management of <span class="searchmatch">Adam</span> Alpert in New York City. Pall, who had grown up DJing, was introduced to`
}
}
render() {
return (
<div dangerouslySetInnerHTML={{ __html: this.state.snippet }} />
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='root'></div>
If you want to display obj.snippet in a div with a specified id, this would be as simple as
HTML
<div id="myDiv"></div>
JS
document.getElementById('myDiv').innerHTML = obj.snippet;
» npm install react-html-converter
I asked this question earlier but seems i didn't frame it properly. So here is the problem. There is some storage which stores html files with some content in it. The storage is done using an API which accepts html string, created a file and stores the html string as .html file.
Now my problem on React side is, there is an input box where user types the content which I want to convert to html code. When user types it, it is just plain text. What I want is a Library that course go through the text and place p, div, h1 etc tags in appropriate places so that I can then send this as html string to the API.
If you have html in a string, you can use dangerouslySetInnerHTML to render it as html.
return (
<div>
{models.map(model => (
<a href="/sofa">
<div className="Parcelas" key={model.id}>
<img
src={"url" + model.image}
className="ParcImage"
alt="sofa"
/>
<h1>Sofá {model.name}</h1>
<h2>
1,200<span>€</span>
</h2>
<p
className="Features"
dangerouslySetInnerHTML={{ __html: model.description }}
/>
<button className="Botao">
<p className="MostraDepois">Ver Detalhes</p>
<span>+</span>
</button>
<img src="../../img/points.svg" className="Decoration" alt="points" />
</div>
</a>
))}
</div>
);
Check if the text you're trying to append to the node is no escaped like this:
model: {
description: '<h1>Hi there!</h1>'
}
Instead of this:
model: {
description: '<h1>Hi there!</h1>'
}
if is escaped you should convert it from your server-side.
if you can't try something like this:
<p className="Features">{model.description.fromCharCode(183)}</p>
Another option is a combination of ReactHtmlParser and unescapingHtml:
import ReactHtmlParser from "react-html-parser";
let model = [
{
description: "<h1>Hello There</h1>"
},
{
description: "<h1>Hello There</h1>"
}
];
function App() {
function unescapeHTML(html) {
var escapeEl = document.createElement("textarea");
escapeEl.innerHTML = html;
return escapeEl.textContent;
}
return (
<div className="App">
{model.map(des => {
return ReactHtmlParser(unescapeHTML(des.description));
})}
</div>
);
}