将字符串渲染为 React 组件 [英] Rendering a string as React component

查看:55
本文介绍了将字符串渲染为 React 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个字符串作为我从另一个页面动态接收的输入来呈现反应组件.但是我会有反应组件的参考.这是例子

I want to render the react components with a string as the input which i am receiving dynamically from another page. BUt i will have the references for the react components. Here is the example

Page1:
-----------------------------
loadPage('<div><Header value=signin></Header></div>');

Page2:
--------------------------------
var React =require('react');
var Header = require('./header');

var home = React.createClass({

loadPage:function(str){ 

           this.setState({
             content : str 
           });

  },

 render : function(){

  return {this.state.content}
 }

});

在此示例中,我将 Header 组件作为字符串接收,并且在我的接收页面中有 Header 组件的引用.我如何用实际的反应组件替换字符串

In this example i am receiving Header component as string , and i have the reference of Header component in my receiving page . How can i substitute the string with the actual react component

推荐答案

这个 react-jsx-parser 组件看起来可以解决您的问题

This react-jsx-parser component looks like it will solve your problem

这篇关于将字符串渲染为 React 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆