2017-09-25 15:24:16 +08:00
---
2017-10-11 10:29:45 +08:00
order: 7
2017-09-25 15:24:16 +08:00
title:
zh-CN: 滚动加载无限长列表
2017-10-13 11:40:49 +08:00
en-US: Infinite & virtualized
2017-09-25 15:24:16 +08:00
---
## zh-CN
结合 [react-virtualized ](https://github.com/bvaughn/react-virtualized ) 实现滚动加载无限长列表,带有虚拟化([virtualization](https://blog.jscrambler.com/optimizing-react-rendering-through-virtualization/))功能,能够提高数据量大时候长列表的性能。
2017-10-11 10:29:45 +08:00
`virtualized` 是在大数据列表中应用的一种技术,主要是为了减少不可见区域不必要的渲染从而提高性能,特别是数据量在成千上万条效果尤为明显。[了解更多](https://blog.jscrambler.com/optimizing-react-rendering-through-virtualization/)
2017-09-25 15:24:16 +08:00
## en-US
2019-04-20 06:36:12 +08:00
An example of infinite list & virtualized loading using [react-virtualized ](https://github.com/bvaughn/react-virtualized ). [Learn more ](https://blog.jscrambler.com/optimizing-react-rendering-through-virtualization/ ).
2017-10-11 10:29:45 +08:00
2017-12-10 11:23:45 +08:00
`Virtualized` rendering is a technique to mount big sets of data. It reduces the amount of rendered DOM nodes by tracking and hiding whatever isn't currently visible.
2017-09-25 15:24:16 +08:00
````jsx
2018-11-28 15:00:03 +08:00
import {
List, message, Avatar, Spin,
} from 'antd';
2017-09-25 15:24:16 +08:00
import reqwest from 'reqwest';
import WindowScroller from 'react-virtualized/dist/commonjs/WindowScroller';
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
import VList from 'react-virtualized/dist/commonjs/List';
import InfiniteLoader from 'react-virtualized/dist/commonjs/InfiniteLoader';
2017-10-11 10:29:45 +08:00
const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
2017-09-25 15:24:16 +08:00
class VirtualizedExample extends React.Component {
state = {
data: [],
loading: false,
}
2018-06-27 15:55:04 +08:00
2017-09-25 15:24:16 +08:00
loadedRowsMap = {}
2018-06-27 15:55:04 +08:00
2018-11-28 15:00:03 +08:00
componentDidMount() {
this.fetchData((res) => {
this.setState({
data: res.results,
});
});
}
fetchData = (callback) => {
2017-09-25 15:24:16 +08:00
reqwest({
url: fakeDataUrl,
type: 'json',
method: 'get',
contentType: 'application/json',
success: (res) => {
callback(res);
},
});
}
2018-06-27 15:55:04 +08:00
2017-09-25 15:24:16 +08:00
handleInfiniteOnLoad = ({ startIndex, stopIndex }) => {
let data = this.state.data;
this.setState({
loading: true,
});
for (let i = startIndex; i < = stopIndex; i++) {
// 1 means loading
this.loadedRowsMap[i] = 1;
}
if (data.length > 19) {
message.warning('Virtualized List loaded all');
this.setState({
loading: false,
});
return;
}
2018-11-28 15:00:03 +08:00
this.fetchData((res) => {
2017-10-11 10:29:45 +08:00
data = data.concat(res.results);
2017-09-25 15:24:16 +08:00
this.setState({
data,
loading: false,
});
});
}
2018-06-27 15:55:04 +08:00
2018-11-28 15:00:03 +08:00
isRowLoaded = ({ index }) => !!this.loadedRowsMap[index]
2018-06-27 15:55:04 +08:00
2017-09-25 15:24:16 +08:00
renderItem = ({ index, key, style }) => {
const { data } = this.state;
const item = data[index];
return (
< List.Item key = {key} style = {style} >
< List.Item.Meta
avatar={< Avatar src = "https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" / > }
2017-10-11 10:29:45 +08:00
title={< a href = "https://ant.design" > {item.name.last}< / a > }
description={item.email}
2017-09-25 15:24:16 +08:00
/>
2017-09-26 16:11:35 +08:00
< div > Content< / div >
2017-09-25 15:24:16 +08:00
< / List.Item >
);
}
2018-06-27 15:55:04 +08:00
2017-09-25 15:24:16 +08:00
render() {
const { data } = this.state;
2018-11-28 15:00:03 +08:00
const vlist = ({
height, isScrolling, onChildScroll, scrollTop, onRowsRendered, width,
}) => (
2017-09-25 15:24:16 +08:00
< VList
autoHeight
height={height}
isScrolling={isScrolling}
onScroll={onChildScroll}
overscanRowCount={2}
rowCount={data.length}
2017-10-11 10:29:45 +08:00
rowHeight={73}
2017-09-25 15:24:16 +08:00
rowRenderer={this.renderItem}
onRowsRendered={onRowsRendered}
scrollTop={scrollTop}
width={width}
/>
);
2018-11-28 15:00:03 +08:00
const autoSize = ({
height, isScrolling, onChildScroll, scrollTop, onRowsRendered,
}) => (
2017-09-25 15:24:16 +08:00
< AutoSizer disableHeight >
2018-11-28 15:00:03 +08:00
{({ width }) => vlist({
height, isScrolling, onChildScroll, scrollTop, onRowsRendered, width,
})}
2017-09-25 15:24:16 +08:00
< / AutoSizer >
);
2018-11-28 15:00:03 +08:00
const infiniteLoader = ({
height, isScrolling, onChildScroll, scrollTop,
}) => (
2017-09-25 15:24:16 +08:00
< InfiniteLoader
isRowLoaded={this.isRowLoaded}
loadMoreRows={this.handleInfiniteOnLoad}
rowCount={data.length}
>
2018-11-28 15:00:03 +08:00
{({ onRowsRendered }) => autoSize({
height, isScrolling, onChildScroll, scrollTop, onRowsRendered,
})}
2017-09-25 15:24:16 +08:00
< / InfiniteLoader >
);
return (
< List >
2017-10-11 10:29:45 +08:00
{
data.length > 0 & & (
2018-04-14 11:18:34 +08:00
< WindowScroller >
2017-10-11 10:29:45 +08:00
{infiniteLoader}
< / WindowScroller >
)
}
{this.state.loading & & < Spin className = "demo-loading" / > }
2017-09-25 15:24:16 +08:00
< / List >
);
}
}
ReactDOM.render(< VirtualizedExample / > , mountNode);
````
2017-10-11 10:29:45 +08:00
````css
.demo-loading {
position: absolute;
bottom: -40px;
left: 50%;
}
````