简体   繁体   English

如何渲染以字符串形式存储在变量中的react组件?

[英]How do i render react component stored as string in variable?

I have Code Login code What i actually need, is to store a whole login.js react component file as a string in the database and then fetch file from db and render that to browser. 我有代码登录代码我真正需要的是将整个login.js react组件文件作为字符串存储在数据库中,然后从db获取文件并将其呈现给浏览器。

I can not render from server since it will take lot of request to do that because the file will be edited by user also will not be real time. 我无法从服务器进行渲染,因为这样做将需要很多请求,因为文件将由用户编辑,也将不是实时的。 Please guide me. 请指导我。

Can it be possible? 有可能吗

It is usually frowned upon and should only be done if you are fully in control of what goes inside the string variable. 它通常会被皱眉,只有在完全控制字符串变量中的内容时,才应该这样做。 But you can do something like this: 但是您可以执行以下操作:

<div dangerouslySetInnerHTML={ __html: A } />;

you wouldn't need to do any of the json stringify or json parse stuff either just would send in as a regular html string. 您不需要做任何json stringify或json parse东西,只需将其作为常规html字符串发送即可。

here is more info on it from React docs https://facebook.github.io/react/docs/dom-elements.html#dangerouslysetinnerhtml 这是来自React docs https://facebook.github.io/react/docs/dom-elements.html#dangerouslysetinnerhtml的更多信息

The reason you need to ensure you are in control of what goes in it is because there is no sanitization of the html and it could be a huge security issue. 您需要确保自己能够控制其中的原因是因为没有对html进行清理,这可能是一个巨大的安全问题。 But if you know exactly what will go in or out of it then there isn't an issue there. 但是,如果您确切知道将要进出的内容,那么这里就没有问题了。

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

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