ReactDOM to render multiple elements using the same class at ones?

对着背影说爱祢 提交于 2019-12-22 09:49:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!