0

I have a document like

var target = document.getElementById('state')

As we append another div with id="data" like

target.appendChild(document.getElementById('data'))

I want to append a component <Test /> to the target. How can I do that? Thanks in advance.

1
  • I would create new element, append it to target and use that element in ReactDOM.render(). Commented Mar 20, 2019 at 10:19

3 Answers 3

1

This is not react way of doing things, in react you make components to have states or receive props. In your case to append another div is conditional, so in your react component you can maintain a state and based on that state render the <Test/> component

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

Comments

0

I think You have to use a loop to render or create dynamic Elements in html like:

<div>
   {testsData.map(data => {
      return (<Test {...data} />);
   })}
</div>

Comments

0

I suggest doing it in the normal react way.

doing as you wish is exposing your site to attacks.

here is an example how to do it dangerously example

here is why not to do it react official dangerously

2 Comments

If this is not secured, could you suggest a secured way?
Its to long to explain I can only tell you that in react you dont have a reason to use query(getElement...) you can use "ref" attribute in the element to ref it

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.