I am trying to create a single-page app blog using React. I want to store my blog posts in a database. Typically, you can just store a blog post in the database as html and render it into a page, but I can't think of how to get that to work for React. Here is some code showing roughly what I want to do:
class BlogPost extends React.Component {
componentDidMount() {
// pretend this string comes from the server
// and MyCustomTextField is defined elsewhere
var blogPostString = "<MyCustomTextField content="some content" />";
// render the react components into the page
ReactDOM.render(blogPostString, this.refs.someRef);
}
render() {
return (
<div ref="someRef"></div>
);
}
}
Obviously this doesn't work since it would need to be converted from jsx, but I was wondering if there is a way to do something along these lines, or how else you would recommend doing it.
Another option I thought of is to store the blog post as an array of JSON objects where each entry includes the name of the component and its attributes, then creating them from the name similarly to this with a lookup table for the name to component mapping.
Ultimately I feel like none of the ideas I've come up with are ideal, so any suggestions for how to do this would be appreciated.