3

I want to dynamically add few div elements just when component is mounted. I tried with following code

<div className="resumebuilder__structure" ref="panel_holder">
</div>

My function which is called in lifecycle hook

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;
    });
}

lifecycle method

componentDidMount() {
  this.refs.panel_holder.appendChild(this.getPanels());
}

But I am getting following error

Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

Does anyone has any idea?

---- Update--

Although I continued with other methods but the answer is still not found. Please do comment if you know anything.

1 Answer 1

0

A better approach would be to put the components in an array in the parent's state. Then render all the components in that array.

Sign up to request clarification or add additional context in comments.

8 Comments

I want to access those div every now and then how do I access it then?
what do you mean by access ?
I will dynamically add some more div but on user click
yes you will add your components to the array and it will rerender using setState().
used case is like: In the beginning I will add some partition in the div which I am trying now and then after when user drag some component over these divs those components will get added as child to particular div element
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.