2017-10-11 10:29:45 +08:00
|
|
|
---
|
|
|
|
order: 2
|
|
|
|
title:
|
|
|
|
zh-CN: 加载更多
|
2017-10-13 11:40:49 +08:00
|
|
|
en-US: Load more
|
2017-10-11 10:29:45 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
可通过 `loadMore` 属性实现加载更多功能。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2017-10-13 11:40:49 +08:00
|
|
|
Load more list with `loadMore` property.
|
2017-10-11 10:29:45 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2022-05-17 10:19:38 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2019-05-07 14:57:32 +08:00
|
|
|
import { List, Avatar, Button, Skeleton } from 'antd';
|
2017-10-11 10:29:45 +08:00
|
|
|
|
2018-08-22 23:34:36 +08:00
|
|
|
const count = 3;
|
2021-10-14 12:09:07 +08:00
|
|
|
const fakeDataUrl = `https://randomuser.me/api/?results=${count}&inc=name,gender,email,nat,picture&noinfo`;
|
2017-10-11 10:29:45 +08:00
|
|
|
|
2022-05-17 10:19:38 +08:00
|
|
|
export default () => {
|
|
|
|
const [initLoading, setInitLoading] = useState(true);
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [data, setData] = useState([]);
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-17 10:19:38 +08:00
|
|
|
useEffect(() => {
|
2021-12-03 23:54:19 +08:00
|
|
|
fetch(fakeDataUrl)
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(res => {
|
2022-05-17 10:19:38 +08:00
|
|
|
setInitLoading(false);
|
|
|
|
setData(res.results);
|
2017-10-11 10:29:45 +08:00
|
|
|
});
|
2022-05-17 10:19:38 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Resetting window's offsetTop so as to display react-virtualized demo underfloor.
|
|
|
|
// In real scene, you can using public method of react-virtualized:
|
|
|
|
// https://stackoverflow.com/questions/46700726/how-to-use-public-method-updateposition-of-react-virtualized
|
|
|
|
window.dispatchEvent(new Event('resize'));
|
|
|
|
}, [JSON.stringify(data)]);
|
|
|
|
|
|
|
|
const onLoadMore = () => {
|
|
|
|
setLoading(true);
|
|
|
|
const newData = data.concat(
|
|
|
|
[...new Array(count)].map(() => ({ loading: true, name: {}, picture: {} })),
|
|
|
|
);
|
|
|
|
setData(newData);
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2021-12-03 23:54:19 +08:00
|
|
|
fetch(fakeDataUrl)
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(res => {
|
2022-05-17 10:19:38 +08:00
|
|
|
setLoading(false);
|
|
|
|
setData(data.concat(res.results));
|
2021-12-03 23:54:19 +08:00
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-17 10:19:38 +08:00
|
|
|
const loadMore =
|
|
|
|
!initLoading && !loading ? (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
textAlign: 'center',
|
|
|
|
marginTop: 12,
|
|
|
|
height: 32,
|
|
|
|
lineHeight: '32px',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Button onClick={onLoadMore}>loading more</Button>
|
|
|
|
</div>
|
|
|
|
) : null;
|
2018-08-22 23:34:36 +08:00
|
|
|
|
2022-05-17 10:19:38 +08:00
|
|
|
return (
|
|
|
|
<List
|
|
|
|
className="demo-loadmore-list"
|
|
|
|
loading={initLoading}
|
|
|
|
itemLayout="horizontal"
|
|
|
|
loadMore={loadMore}
|
|
|
|
dataSource={data}
|
|
|
|
renderItem={item => (
|
|
|
|
<List.Item
|
|
|
|
actions={[<a key="list-loadmore-edit">edit</a>, <a key="list-loadmore-more">more</a>]}
|
|
|
|
>
|
|
|
|
<Skeleton avatar title={false} loading={item.loading} active>
|
|
|
|
<List.Item.Meta
|
|
|
|
avatar={<Avatar src={item.picture.large} />}
|
|
|
|
title={<a href="https://ant.design">{item.name.last}</a>}
|
|
|
|
description="Ant Design, a design language for background applications, is refined by Ant UED Team"
|
|
|
|
/>
|
|
|
|
<div>content</div>
|
|
|
|
</Skeleton>
|
|
|
|
</List.Item>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|
2017-10-11 10:29:45 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```css
|
2017-10-11 10:29:45 +08:00
|
|
|
.demo-loadmore-list {
|
2017-10-18 17:16:14 +08:00
|
|
|
min-height: 350px;
|
2017-10-11 10:29:45 +08:00
|
|
|
}
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|