I need to render an HTML (JSX) string in a React class. I don't know if this is possible or not.
dangerouslySetInnerHTML is not valid for me because I have different react components inside this file. It's not plain HTML.
I have an example with the expected result:
https://jsfiddle.net/86rg50re/1/
var MyComponent = React.createClass({
propTypes: {
owner: React.PropTypes.string
},
render: function() {
return <div>Congrats {this.props.owner}! you have rendered MyComponent ({this.props.children})</div>;
}
});
var Hello = React.createClass({
render: function() {
return <div>Header <MyComponent owner={"Daniel"}>Yayyyyyy!</MyComponent></div>;
}
});
But what I have is this:
var Hello = React.createClass({
render: function() {
var content = '<div>Header <MyComponent owner={"Daniel"}>Yayyyyyy!</MyComponent></div>';
return transformStringToJSX(content);
}
Obviously transformStringToJSX doesn't exists.
Is there a way to render jsx strings?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…