admin管理员组文章数量:1244258
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 badges2 Answers
Reset to default 7As 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"));
本文标签: javascriptRender multiple React components into a single DOM elementStack Overflow
版权声明:本文标题:javascript - Render multiple React components into a single DOM element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740216760a2243010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论