2017-09-06 15:53:25 +08:00
|
|
|
---
|
2017-10-11 10:29:45 +08:00
|
|
|
order: 6
|
2017-09-06 15:53:25 +08:00
|
|
|
title:
|
2017-09-25 15:24:16 +08:00
|
|
|
zh-CN: 滚动加载
|
|
|
|
en-US: Scrolling loaded List
|
2017-09-06 15:53:25 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
2017-09-25 15:24:16 +08:00
|
|
|
结合 [react-infinite-scroller](https://github.com/CassetteRocks/react-infinite-scroller) 实现滚动自动加载列表。
|
2017-09-06 15:53:25 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2017-09-25 15:24:16 +08:00
|
|
|
The example of infinite load with [react-infinite-scroller](https://github.com/CassetteRocks/react-infinite-scroller).
|
2017-09-06 15:53:25 +08:00
|
|
|
|
|
|
|
````jsx
|
2017-09-25 15:24:16 +08:00
|
|
|
import { List, message, Avatar, Spin } from 'antd';
|
|
|
|
import reqwest from 'reqwest';
|
2017-09-06 15:53:25 +08:00
|
|
|
|
2017-09-25 15:24:16 +08:00
|
|
|
import InfiniteScroll from 'react-infinite-scroller';
|
2017-09-06 15:53:25 +08:00
|
|
|
|
2017-10-11 10:29:45 +08:00
|
|
|
const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
|
2017-09-06 15:53:25 +08:00
|
|
|
|
2017-09-25 15:24:16 +08:00
|
|
|
class InfiniteListExample extends React.Component {
|
2017-09-06 15:53:25 +08:00
|
|
|
state = {
|
2017-09-25 15:24:16 +08:00
|
|
|
data: [],
|
2017-09-06 15:53:25 +08:00
|
|
|
loading: false,
|
2017-09-25 15:24:16 +08:00
|
|
|
hasMore: true,
|
|
|
|
}
|
|
|
|
getData = (callback) => {
|
|
|
|
reqwest({
|
|
|
|
url: fakeDataUrl,
|
|
|
|
type: 'json',
|
|
|
|
method: 'get',
|
|
|
|
contentType: 'application/json',
|
|
|
|
success: (res) => {
|
|
|
|
callback(res);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
componentWillMount() {
|
|
|
|
this.getData((res) => {
|
|
|
|
this.setState({
|
2017-10-11 10:29:45 +08:00
|
|
|
data: res.results,
|
2017-09-25 15:24:16 +08:00
|
|
|
});
|
|
|
|
});
|
2017-09-06 15:53:25 +08:00
|
|
|
}
|
2017-09-25 15:24:16 +08:00
|
|
|
handleInfiniteOnLoad = () => {
|
2017-09-06 15:53:25 +08:00
|
|
|
let data = this.state.data;
|
|
|
|
this.setState({
|
|
|
|
loading: true,
|
|
|
|
});
|
2017-09-25 15:24:16 +08:00
|
|
|
if (data.length > 14) {
|
|
|
|
message.warning('Infinite List loaded all');
|
|
|
|
this.setState({
|
|
|
|
hasMore: false,
|
|
|
|
loading: false,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.getData((res) => {
|
2017-10-11 10:29:45 +08:00
|
|
|
data = data.concat(res.results);
|
2017-09-06 15:53:25 +08:00
|
|
|
this.setState({
|
|
|
|
data,
|
|
|
|
loading: false,
|
|
|
|
});
|
2017-09-25 15:24:16 +08:00
|
|
|
});
|
2017-09-06 15:53:25 +08:00
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return (
|
2017-10-11 10:29:45 +08:00
|
|
|
<div className="demo-infinite-container">
|
|
|
|
<InfiniteScroll
|
|
|
|
initialLoad={false}
|
|
|
|
pageStart={0}
|
|
|
|
loadMore={this.handleInfiniteOnLoad}
|
|
|
|
hasMore={!this.state.loading && this.state.hasMore}
|
|
|
|
useWindow={false}
|
2017-09-25 15:24:16 +08:00
|
|
|
>
|
2017-10-11 10:29:45 +08:00
|
|
|
<List
|
|
|
|
dataSource={this.state.data}
|
|
|
|
renderItem={item => (
|
|
|
|
<List.Item key={item.id}>
|
|
|
|
<List.Item.Meta
|
|
|
|
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 && <Spin className="demo-loading" />}
|
|
|
|
</List>
|
|
|
|
</InfiniteScroll>
|
|
|
|
</div>
|
2017-09-06 15:53:25 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 15:24:16 +08:00
|
|
|
ReactDOM.render(<InfiniteListExample />, mountNode);
|
2017-10-11 10:29:45 +08:00
|
|
|
````
|
|
|
|
|
|
|
|
````css
|
|
|
|
.demo-infinite-container {
|
|
|
|
border: 1px solid #eee;
|
|
|
|
overflow: auto;
|
|
|
|
padding: 48px 16px;
|
|
|
|
height: 300px;
|
|
|
|
}
|
|
|
|
.demo-loading {
|
|
|
|
position: absolute;
|
|
|
|
bottom: -40px;
|
|
|
|
left: 50%;
|
|
|
|
}
|
|
|
|
````
|