您可能需要考虑使用像这样的“虚拟”列表库:https ://github.com/bvaughn/react-virtualized
通常,这些库为您提供了一种创建自定义列表容器和自定义列表项的方法。
这是如何执行此操作的示例代码:
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";
// NOTE: Your import path for react-virtualized may be different if you are
// using TypeScript or ES6/Babel or UMD modules.
import { List } from "react-virtualized";
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
ids: ["abc", "def", "ghi"]
};
}
// the rowRenderer function is given an object with a lot more stuff
// in it: https://github.com/bvaughn/react-virtualized/blob/master/docs/List.md
rowRenderer = ({ key, index, style }) => {
const value = this.state.ids[index];
return (
<ListItem key={key} style={style}>
<ListItemText>{value}</ListItemText>
</ListItem>
);
}
render() {
return (
<List
height={150} width={250} rowHeight={30}
rowCount={this.state.ids.length}
rowRenderer={this.rowRenderer} />
);
}
}