javascript - 我们可以根据条件在 react.js 中的 map 中引入换行符(css)吗
问题描述
我有一个数组,我试图在数组的每四个元素中引入一个换行符,以便每四个元素在一起,而下一行将包含接下来的 4 个元素,依此类推。
this.state.colors: ['red', 'brown', 'yellow', 'blue', 'black', 'pink', 'green', 'aliceblue',
'gray', 'magenta', 'orange', 'violet','coral', 'white', 'olive', 'lightgreen']
<ul className="colorsList">
{this.state.colors.map((color, index) => (
<div>
<div className="flex">
<div style={{ background: color, height: 20, width: 20 }}></div>
</div>
{(index === 3 || 7 || 11) && <br></br>}
</div>
))}
</ul>
应该是这样的。。
[][][][]
[][][][]
[][][][]
这里的括号表示这些颜色框的高度和宽度为 20、20。
解决方案
我不想让 javascript 来解决我遇到的这个问题,并想用纯 CSS 来做。这是我的解决方案:)
this.state.colors: ['red', 'brown', 'yellow', 'blue', 'black', 'pink', 'green', 'aliceblue',
'gray', 'magenta', 'orange', 'violet','coral', 'white', 'olive', 'lightgreen']
<ul className="colorsList">
{this.state.colors.map((color, index) => (
<div>
{(index % 4 === 0 ) ?
<div>
<div style={{ clear: 'both' }}></div>
<div style={{ background: color, height: 20, width: 20, float: 'left' }}></div>
</div>
:
<div style={{ background: color, height: 20, width: 20, float: 'left' }}></div>
}
</div>
))}
</ul>
推荐阅读
- javascript - 引导多选:默认值和 onChange 不起作用
- git - 使用 ssh-agent 调用 docker-compose
- javascript - 未捕获(承诺中)类型错误:无法读取未定义的属性“绑定”
- python - 将系列作为行应用于所有行中具有相同系列的熊猫数据框
- mongodb - 无法使用 findById 在 expressJs 中读取 mongo 数据
- arrays - 数组类型的变量或表达式的值是数组元素零的地址
- java - 如何比较从 Web 服务中提取的 3 个变量(equalsIgnoreCase)?
- ios - 从块内回调块
- c# - 如何使用 linq 正确填充此字符串数组?
- node.js - npm 错误!appium-chromedriver@3.1.4 安装:`node install-npm.js`