最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Render multiple React components into a single DOM element - Stack Overflow

programmeradmin3浏览0评论

I'm looking to render multiple modals into a single ReactDOM element. Here's the HTML structure that React renders to.

<body>
    <div id="modal-socket"></div> // Insert multiple here
    <div id="wrapper">
        // Other content goes here
    </div>
</body>

There's a long story behind why I need to render multiple ponents into #modal-socket but I want to do something akin to this:

ReactDOM.render(<AddMeasurableModal />, document.getElementById("modal-socket"));
ReactDOM.render(<AddMeasurableModal />, document.getElementById("modal-socket"));
ReactDOM.render(<AddMeasurableModal />, document.getElementById("modal-socket"));

Obviously this replaces the current content of #modal-socket on each render call.. So I don't get my end result. Boo.

Did a search and found a few answers on it but none meet my needs.

Cheers.

I'm looking to render multiple modals into a single ReactDOM element. Here's the HTML structure that React renders to.

<body>
    <div id="modal-socket"></div> // Insert multiple here
    <div id="wrapper">
        // Other content goes here
    </div>
</body>

There's a long story behind why I need to render multiple ponents into #modal-socket but I want to do something akin to this:

ReactDOM.render(<AddMeasurableModal />, document.getElementById("modal-socket"));
ReactDOM.render(<AddMeasurableModal />, document.getElementById("modal-socket"));
ReactDOM.render(<AddMeasurableModal />, document.getElementById("modal-socket"));

Obviously this replaces the current content of #modal-socket on each render call.. So I don't get my end result. Boo.

Did a search and found a few answers on it but none meet my needs.

Cheers.

Share Improve this question asked May 22, 2016 at 19:24 DezachuDezachu 1531 gold badge2 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

As you told in a ment, the dynamic way would be something like this Inside of a main ponent you could do:

Imagine having an array like:

let myArray = [
  {
     prop1: 'hello world'
  },
  {
     prop1: 'Hey there!'
  }
]

//Then in the render function (you can put that array into the state or something)
render(){
      return (
         <div>
           {myArray.map((entry,index) => {
              return <AddMeasurableModal key={index} {...entry} />
           })}
         </div>
      )
}

this will create as many AddMeasurableModal ponents as there are entrys in the myArray variable and add every property stored as props onto the ponent (In this case, every AddMeasurableModal ponent has access to the this.props.prop1 value, because of the {...entry} spread syntax)

Notice how I only put myArray.map() into the render function inside of {}? React renders every array of ponents without further configuration inside of the render function. And Array.map() returns an array. Just make sure to return only valid react elements! When doing this, don't forget to add a uniqe key prop to each element to avoid warnings.

EDIT: in this case, the key prop is the current index in the array, but when fetching data from a server I would remend to use a uniqe id from the database or something to avoid rendering bugs. If you don't want to map over an array, you can just set a number of ponents and then loop over them, creating an array of ponents and put them into the render function.

Wrap your multiple modals into 1 container and render that, eg:

let modals = (
  <div>
    <AddMeasurableModal />
    <AddMeasurableModal />
    <AddMeasurableModal />
  </div>
);

ReactDOM.render(modals, document.getElementById("modal-socket"));
发布评论

评论列表(0)

  1. 暂无评论