简体   繁体   English

在React.js中,如何从组件的字符串表示中呈现组件?

[英]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. 它们不仅仅是html字符串,就像stackoverflow中的一些其他示例一样。

How might one go about doing this? 怎么可能这样做呢? Is it even possible? 它甚至可能吗?

You can use dangerouslySetInnerHTML , documented here . 您可以使用dangerouslySetInnerHTML此处记录

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

This is also discussed here . 这也在这里讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM