javascript - React render array of objects in cells of Bootstrap grid
问题描述
I have an array of components and I want to render each element in a different cell. The length of the array can be always different and can be more then 12 elements that are the limit of the cells in the Bootstrap grid system. So if the elements are more then 12 I need to create a new rows.
In my RENDER method I have this:
let components = this.state.elements.map(
(currElement, index) => this.renderElement(currElement, index)
);
return (
<div className="wrap-content container-fluid">
<section id="box-main" className="container-fluid">
<div className="row flex-items-xs-center">
<div className="col-xs">
<header>
<h4>{this._title}</h4>
</header>
</div>
</div>
{components}
</section>
</div>);
The method renderElement now render each element in a row, but I want to render in a cell but I don't know how to do. The ElementItem is a component that render the single element.
renderElement(element, index)
{
return(
<div key={index} className="row flex-items-xs-center">
<div className="col-xs">
<ElementItem element={element} propA={element.propA} template={2} />
</div>
</div>
);
}
解决方案
Christian, I'm not sure if I understood your question. But if you want each element rendered in a column(cell) you must declare your components inside row
and then specify the breakpoints. Something like this:
const MainComponent = props =>{
const elements = elements.map(element =>{
return(<div className="col md-4"> {element} </div>);
})
return(
<div className="row">
{elements}
</div>
)
}
推荐阅读
- python - 在点击中弃用参数别名的正确方法
- javascript - jQuery ajax async 'true' 让我的网络应用程序卡住,直到数据返回
- android - 无法在 sdk 更新站点中更新存储库
- c# - C# 更新网络适配器名称部分工作(WinReg 好,ipconfig 坏)
- android - Base.Widget.Design.TabLayout 不能作为 android 中 style.xml 中的父级
- c++ - 在c ++ stl map中,删除具有重复值的条目
- javascript - 用 combineLatest 匹配两个 Observable 数据对象
- webpack - webpack 4.5 设置代理公司
- javascript - 多人游戏 NodeJS、HTML、JS - 服务器/客户端验证
- c# - 实体类型“Microsoft.AspNetCore.Identity.IdentityRole”处于阴影状态