多亏了多伦的评论,我才成功。
这是修改过的代码。
const Container = {
width: "90%",
height: "100vh",
margin: "auto",
};
class App extends Component {
state = {
data: [],
sort: {
column: "",
descending: false,
},
};
componentDidMount() {
const numbers = [];
for (let i = 0; i < 100; i++) {
numbers.push(i + 1);
}
const final = numbers.map(n => {
return {
single: n,
double: n * 2,
triple: n * 3
};
});
this.setState({ data: final });
}
cellRenderer = (data, columns) => {
if (data.rowIndex === 0) {
return (
<span
style={data.style}
key={data.key}
>
{columns[data.columnIndex]}
</span>
);
}
const column = columns[data.columnIndex];
return (
<span
style={data.style}
key={data.key}
>
{this.state.data[data.rowIndex - 1][column]}
</span>
);
}
render() {
const columns = ["single", "double", "triple"];
return (
<div style={Container}>
<AutoSizer>
{({ width, height }) => (
<MultiGrid
columnWidth={70}
width={width}
height={height}
cellRenderer={(data) => this.cellRenderer(data, columns)}
fixedRowCount={1}
rowHeight={70}
columnCount={3}
rowCount={this.state.data.length + 1}
/>
)}
</AutoSizer>
</div>
);
}
}
注意,现在的总行数实际上比数据数组的长度多1,然后在我的
cellRenderer
,I索引比当前索引少1。