2 回答

TA贡献1844条经验 获得超8个赞
下面是另一个选项(我更喜欢可读性)。
render() {
return (
<div>
{Array(3).fill().map((_, index) => (
<div class="row">
{this.renderSquare(index * 3)}
{this.renderSquare(index * 3 + 1)}
{this.renderSquare(index * 3 + 2)}
</div>
))}
</div>
);
}
如果您希望行数/方块数是动态的,您可以使用以下内容:
render() {
const rowCount = 3;
const squaresPerRow = 3;
return (
<div>
{Array(rowCount).fill().map((_, rowIndex) => (
<div class="row">
{Array(squaresPerRow).fill().map((_, squareIndex) => (
this.renderSquare(rowIndex * squaresPerRow + squareIndex)
))}
</div>
))}
</div>
);
}

TA贡献1826条经验 获得超6个赞
我认为嵌套循环是最简单的解决方案:
render() {
const squareItems = [];
for (var i=0; i < 3; i++) {
squareItems.push(<div class="row">
{Array(3).map((_, j) => this.renderSquare(i * 3 + j))}
</div>);
}
return (
<div>
{ squareItems }
</div>
);
}
添加回答
举报