meesterguyperson
meesterguyperson

Reputation: 1884

In React.js, how would I render a component from a string representation of a component?

My problem specifically is that I would like to be able to create dynamic templates, depending on the type of item contained in a certain record. I would like to store these components as strings in the database and render them dynamically as the items are loaded. The templates would need to be able to handle dynamic react variables. They are not just html strings as in some of the other examples on stackoverflow.

How might one go about doing this? Is it even possible?

Upvotes: 3

Views: 830

Answers (1)

Jakemmarsh
Jakemmarsh

Reputation: 4661

You can use dangerouslySetInnerHTML, documented here.

function createMarkup() { return {__html: 'First · Second'}; };
<div dangerouslySetInnerHTML={createMarkup()} />

This is also discussed here.

Upvotes: 1

Related Questions