避免使用
dangerouslySetInnerHtml
<Col>
在一个
<Row>
. 下一步,按这个
<
具有
children
变成一个
chunked
数组。所有数据分块后,React将呈现此
大块的
整个阵列。
大块的
数组将如下所示:
[
[
<Row>
<Col/>
<Col/>
<Col/>
</Row>
],
[
<Row>
<Col/>
<Col/>
<Col/>
</Row>
]
...etc
]
columns
大小,然后设置
柱
1,2,3,4,6,8,12
)在
<RenderColors columns={3}/>
位于
应用程序.js
工作示例:
https://codesandbox.io/s/30v7qvoz3m
import map from "lodash/map";
import React from "react";
import { Col, Row } from "antd";
export default ({ columns, data }) => {
const chunked = [];
let key = 0;
let beginIndex = 0;
let endIndex = columns;
while (key <= Math.ceil(data.length / columns)) {
chunked.push(
<Row key={key}>
{map(
data.slice(beginIndex, endIndex),
({ color, background, name }) => (
<Col
style={{ background, height: 75 }}
key={name}
span={24 / columns}
>
<div style={{ color, padding: 20, textTransform: "uppercase" }}>
{name}
</div>
</Col>
)
)}
</Row>
);
beginIndex = beginIndex + columns;
endIndex = endIndex + columns;
key++;
}
return chunked;
};
组件/App.js
import React from "react";
import RenderColors from "./renderColors";
import colors from "./colors";
export default () => (
<div className="container">
<h1 className="title">Dynamic Rows</h1>
<RenderColors columns={3} data={colors} />
</div>
);
export default [
{ background: "#F44539", name: "Red", color: "white" },
{ background: "#E82363", name: "Pink", color: "white" },
{ background: "#9B2BAF", name: "Purple", color: "white" },
{ background: "#673DB6", name: "Deep Purple", color: "white" },
{ background: "#4152B3", name: "Indigo", color: "white" },
{ background: "#2695F3", name: "Blue", color: "white" },
{ background: "#0BA7F4", name: "Light Blue", color: "white" },
{ background: "#00BBD3", name: "Cyan", color: "white" },
{ background: "#009587", name: "Teal", color: "white" },
{ background: "#4DAE51", name: "Green", color: "white" },
{ background: "#8AC24B", name: "Light Green", color: "black" },
{ background: "#CCDB3C", name: "Lime", color: "black" },
{ background: "#FFEA3D", name: "Yellow", color: "black" },
{ background: "#FFC010", name: "Amber", color: "black" },
{ background: "#FF9700", name: "Orange", color: "black" },
{ background: "#FF5827", name: "Deep Orange", color: "white" },
{ background: "#785649", name: "Brown", color: "white" },
{ background: "#9D9D9D", name: "Warm Grey", color: "black" },
{ background: "#607C8A", name: "Cool Grey", color: "white" }
];