问题
I have a multiple elements using the same class and having the same content.
So I can I use ReactDOM to render them at ones instead of:
ReactDOM.render(
<Footer source="./data/nav.json"/>,
document.getElementsByClassName('footer')[0] //mountNode
);
ReactDOM.render(
<Footer source="./data/nav.json"/>,
document.getElementsByClassName('footer')[1] //mountNode
);
ReactDOM.render(
<Footer source="./data/nav.json"/>,
document.getElementsByClassName('footer')[2] //mountNode
);
It would be best if I can do it dynamically as I wouldn't know how many these multiple same elements are.
Is it possible?
EDIT:
var elementLength = document.getElementsByClassName("footer").length;
if (elementLength > 0) {
for (var i = 0; i < elementLength; i++) {
ReactDOM.render(
<Footer source="./data/nav.json"/>,
document.getElementsByClassName('footer')[i] //mountNode
);
}
}
it will call ./data/nav.json
multiple times.
回答1:
Something like (not tested):
Array.prototype.forEach.call(
document.getElementsByClassName('footer'),
function(el) {
ReactDOM.render(
<Footer source="./data/nav.json"/>,
el
)
}
)
edit: now tested
来源:https://stackoverflow.com/questions/40022574/reactdom-to-render-multiple-elements-using-the-same-class-at-ones