ant-design/components/list/demo/infinite-load.md

134 lines
2.9 KiB
Markdown
Raw Normal View History

---
order: 6
title:
zh-CN: 滚动加载
en-US: Scrolling loaded
---
## zh-CN
结合 [react-infinite-scroller](https://github.com/CassetteRocks/react-infinite-scroller) 实现滚动自动加载列表。
## en-US
The example of infinite load with [react-infinite-scroller](https://github.com/CassetteRocks/react-infinite-scroller).
2019-05-07 14:57:32 +08:00
```jsx
import { List, message, Avatar, Spin } from 'antd';
import reqwest from 'reqwest';
import InfiniteScroll from 'react-infinite-scroller';
const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
class InfiniteListExample extends React.Component {
state = {
data: [],
loading: false,
hasMore: true,
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2018-11-28 15:00:03 +08:00
componentDidMount() {
2019-05-07 14:57:32 +08:00
this.fetchData(res => {
2018-11-28 15:00:03 +08:00
this.setState({
data: res.results,
});
});
}
2019-05-07 14:57:32 +08:00
fetchData = callback => {
reqwest({
url: fakeDataUrl,
type: 'json',
method: 'get',
contentType: 'application/json',
2019-05-07 14:57:32 +08:00
success: res => {
callback(res);
},
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
handleInfiniteOnLoad = () => {
2019-06-19 19:09:08 +08:00
let { data } = this.state;
this.setState({
loading: true,
});
if (data.length > 14) {
message.warning('Infinite List loaded all');
this.setState({
hasMore: false,
loading: false,
});
return;
}
2019-05-07 14:57:32 +08:00
this.fetchData(res => {
data = data.concat(res.results);
this.setState({
data,
loading: false,
});
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
render() {
return (
<div className="demo-infinite-container">
<InfiniteScroll
initialLoad={false}
pageStart={0}
loadMore={this.handleInfiniteOnLoad}
hasMore={!this.state.loading && this.state.hasMore}
useWindow={false}
>
<List
dataSource={this.state.data}
renderItem={item => (
<List.Item key={item.id}>
<List.Item.Meta
2019-05-07 14:57:32 +08:00
avatar={
<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
}
title={<a href="https://ant.design">{item.name.last}</a>}
description={item.email}
/>
<div>Content</div>
</List.Item>
)}
>
{this.state.loading && this.state.hasMore && (
<div className="demo-loading-container">
<Spin />
</div>
)}
</List>
</InfiniteScroll>
</div>
);
}
}
ReactDOM.render(<InfiniteListExample />, mountNode);
2019-05-07 14:57:32 +08:00
```
2019-05-07 14:57:32 +08:00
```css
.demo-infinite-container {
2017-10-26 15:36:48 +08:00
border: 1px solid #e8e8e8;
border-radius: 4px;
overflow: auto;
padding: 8px 24px;
height: 300px;
}
.demo-loading-container {
position: absolute;
bottom: 40px;
width: 100%;
text-align: center;
}
2019-05-07 14:57:32 +08:00
```
2019-12-25 17:05:51 +08:00
<style>
[data-theme="dark"] .demo-infinite-container {
border: 1px solid #303030;
}
</style>