首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >动态添加到html元素

动态添加到html元素
EN

Stack Overflow用户
提问于 2018-07-30 02:34:55
回答 1查看 62关注 0票数 3

我想在组件挂载时动态添加几个div元素。我尝试使用以下代码

代码语言:javascript
复制
<div className="resumebuilder__structure" ref="panel_holder">
</div>

我在生命周期钩子中调用的函数

代码语言:javascript
复制
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;
    });
}

生命周期方法

代码语言:javascript
复制
componentDidMount() {
  this.refs.panel_holder.appendChild(this.getPanels());
}

但是我得到了以下错误

代码语言:javascript
复制
Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

有谁知道吗?

-更新

虽然我继续使用其他方法,但仍然没有找到答案。如果你知道什么,请发表评论。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-30 02:48:50

更好的方法是将数组中的组件放在父级的状态中。然后呈现该数组中的所有组件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51583369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档