gpt4 book ai didi

javascript - 动态 HTML 字符串来 react 组件

转载 作者:可可西里 更新时间:2023-11-01 02:05:05 30 4
gpt4 key购买 nike

我将从我的模板渲染中获取动态 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com