【问题标题】:Dynamic Addition to html element动态添加到 html 元素
【发布时间】:2019-01-06 01:53:10
【问题描述】:

我想在安装组件时动态添加一些 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'.

有人知道吗?

---- 更新--

虽然我继续使用其他方法,但仍然没有找到答案。有知道的请留言。

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

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

    【讨论】:

    • 我想时不时访问那些 div,那我该如何访问呢?
    • 访问是什么意思?
    • 我会在用户点击时动态添加更多 div
    • 是的,你会将你的组件添加到数组中,它会使用 setState() 重新渲染。
    • 用例如下:一开始我会在我正在尝试的 div 中添加一些分区,然后当用户将某些组件拖到这些 div 上时,这些组件将作为子组件添加到特定 div元素
    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2011-07-05
    • 2019-02-17
    • 2019-12-29
    • 2018-02-10
    • 1970-01-01
    相关资源
    最近更新 更多