user1432882
user1432882

Reputation: 1152

Can ReactDOM render list of elements

I have a list of elements (Same element) that needs to be rendered multiple times within a page. All the examples I have seen of ReactDOM.render show that it can only render a single element. Is there a way to render a list of elements?

Note: Element may appear all over the page. I cannot make a list class such as <ElementList/> that renders multiple elements.

Vanilla usage:

ReactDOM.render(
<Element/>,
document.getElementById('Element'));

Is there a way to pass in a list of elements or something equivalent instead such as

ReactDOM.render(
<Element/>,
listOfElements);

I know I can write a wrapper function, and call document.getElementById() multiple times, but is there something built-in into react that can handle this situation?

Upvotes: 0

Views: 1768

Answers (1)

madox2
madox2

Reputation: 51841

You can use forEach function:

listOfElements.forEach(el => ReactDOM.render(<Element/>, el);

Upvotes: 3

Related Questions