Is it possible to dynamically render a React component from a string?
Basically I have pages' content coming from a database and I want to have React components within the content. An example of what I'm trying to achieve:
var html_string = '<i>React Component Rendered</i>: <Hello name="World" />';
var Hello = React.createClass({
render: function() {
return <strong>Hello {this.props.name}</strong>;
}
});
function createMarkup() { return {__html: html_string}; };
var App = React.createClass({
render: function() {
return <div dangerouslySetInnerHTML={createMarkup()} />;
}
});
ReactDOM.render(
<App/>,
document.getElementById('container')
);
But it just renders the HTML without evaluating <Hello />
:
<div><i>React Component Rendered</i>: <hello name="World"></hello></div>
I'm trying to get:
<div><i>React Component Rendered</i>: <strong>Hello World</strong></div>
Example on JSFiddle