Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
102
rated 0 times [  105] [ 3]  / answers: 1 / hits: 22862  / 9 Years ago, sun, march 22, 2015, 12:00:00

I use setState to update part of the html but I found the new update html is not rendered. Here is the code, js fiddle:



html:



<div>hello<br />world<br /></div>
<div id='content'></div>


js:



var Main = React.createClass({
getInitialState: function() {
return {output: 'hello<br />world<br />'};
},
render: function() {
return (
<div>
{this.state.output}
</div>
);
}
});

React.renderComponent(<Main/>, document.getElementById('content'));


I omit the part that update html from the server but the result is the same. How to let the string in state be rendered?


More From » html

 Answers
3

Use dangerouslySetInnerHTML to inject HTML as a string in React (but note that React won’t be able to use this markup in its virtual DOM):


<div dangerouslySetInnerHTML={{__html: this.state.output}} />

http://jsfiddle.net/5Y8r4/11/


The API for doing this is deliberately complex because this isn't safe to do routinely. React has protections against XSS, using dangerouslySetInnerHTML circumvents those and opens you up to risk if you aren't sanitizing that data.


[#67354] Thursday, March 19, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
felixa

Total Points: 180
Total Questions: 113
Total Answers: 108

Location: Palau
Member since Sat, Aug 21, 2021
3 Years ago
;