我想在组件挂载时动态添加几个div元素。我尝试使用以下代码
<div className="resumebuilder__structure" ref="panel_holder">
</div>
我在生命周期钩子中调用的函数
getPanels = () => {
return this.props
.resumeTemplate
.roughTemplateStructure
.roughScale.map((roughS) => {
const uid = uuid();
this.updatePanelInTemplate(uid);
const elem = <div ref={uid} className={roughS.class} ></div>;
return elem;
});
}
生命周期方法
componentDidMount() {
this.refs.panel_holder.appendChild(this.getPanels());
}
但是我得到了以下错误
Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
有谁知道吗?
-更新
虽然我继续使用其他方法,但仍然没有找到答案。如果你知道什么,请发表评论。
发布于 2018-07-30 02:48:50
更好的方法是将数组中的组件放在父级的状态中。然后呈现该数组中的所有组件。
https://stackoverflow.com/questions/51583369
复制相似问题