【发布时间】:2017-10-20 06:21:57
【问题描述】:
我正在尝试为使用<script type="text/template"></script> 呈现页面元素的项目做出贡献。
我的贡献是将页面的元素变成了 React 组件。但是,当我使用 ReactDOM.render() 命令在特定 div 中呈现反应时,我收到一条错误消息
未捕获的错误:_registerComponent(...):目标容器不是 DOM 元素。
我知道这意味着 react 找不到要渲染的 div,所以我需要在 div 之后加载 react 脚本,我已经尝试过了,但错误又出现了。
我尝试过像这样加载反应脚本
<script type="text/template">
<ul class="new-component-template" id="xblocklist" >
</ul>
<script type="text/babel" src="path to react file"/>
</script>
但是当我加载页面时,错误消失了,脚本根本没有加载。
我需要的是在调用外部脚本时加载脚本,I.E我可以在<script type="text/template"></script>里面写一个函数来实际加载里面的react脚本。
更新
var ListXblocks = React.createClass({
componentWillMount: function(){
this.setState({Problemstyle: this.props.Problemstyle})
fetch('http://192.168.33.10:8002/XBlocks/')
.then(result=>result.json())
.then(result=> {
this.setState({items:result})
})
},
render:function(){
return(
<div>
{
this.state.items.map((item)=>{return(
<li className="editor-manual" key={item.title}>
<button type="button" className="button-component" data-category="problem" data-boilerplate="">
<span className="name">{item.description}</span>
</button>
</li>)})
}
</div>
);
}
});
ReactDOM.render(<ListXblocks/>, document.getElementById('xblocklist'));
【问题讨论】:
-
你能告诉我们
ReactDOM.render()如何使用ul标签吗? -
@TharakaWijebandara 我应该在
ul标签内渲染组件。我更新了我的帖子并添加了反应代码
标签: javascript reactjs templates openedx