我将从我的模板渲染中获取动态 html 内容,该内容由其他 React 组件渲染。我如何将此 html 字符串转换为 React 组件,以便我可以在渲染函数中使用该组件。请注意,我想保留用于比较的 React 特定属性。
React.createClass( {
var self = this;
componentWillMountDown : function() {
//htmlString is essentially huge dynamic one in my actual case
var htmlString = "<div class='classDiv' react-id="0.1"><input type='text'/></div>";
self.setState({responseString : htmlString});
self.forceUpdate();
},
render: function() {
var Response = this.state.responseString;
//how would I return the react component as response?
return (<Response/>); //does not work. err is it shd be valid react component
}
});
我已经尝试将 htmlString 转换为 HTMLDocument 对象并在 willmount 回调中递归创建 React.createElement 并设置 react 组件。但是,错误是未定义 toUpperCase 类型。
最佳答案
几个小时前,我在 npm ( https://www.npmjs.com/package/html-to-react ) 中发布了一个 html-to-react
模块,可能会对您有所帮助。
如果您有任何需要,请告诉我,我当然可以与您合作,使模块更加灵活。
关于javascript - 动态 HTML 字符串来 react 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30715220/