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: 滚动加载
|
2017-10-13 11:40:49 +08:00
|
|
|
en-US: Scrolling loaded
|
2017-09-06 15:53:25 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
2021-10-13 18:59:17 +08:00
|
|
|
结合 [react-infinite-scroll-component](https://github.com/ankeetmaini/react-infinite-scroll-component) 实现滚动自动加载列表。
|
2017-09-06 15:53:25 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2021-10-13 18:59:17 +08:00
|
|
|
The example of infinite load with [react-infinite-scroll-component](https://github.com/ankeetmaini/react-infinite-scroll-component).
|
2017-09-06 15:53:25 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-05-21 22:14:15 +08:00
|
|
|
import { Avatar, Divider, List, Skeleton } from 'antd';
|
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-10-13 18:59:17 +08:00
|
|
|
import InfiniteScroll from 'react-infinite-scroll-component';
|
2017-09-06 15:53:25 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
interface DataType {
|
|
|
|
gender: string;
|
|
|
|
name: {
|
|
|
|
title: string;
|
|
|
|
first: string;
|
|
|
|
last: string;
|
|
|
|
};
|
|
|
|
email: string;
|
|
|
|
picture: {
|
|
|
|
large: string;
|
|
|
|
medium: string;
|
|
|
|
thumbnail: string;
|
|
|
|
};
|
|
|
|
nat: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const App: React.FC = () => {
|
2021-10-13 18:59:17 +08:00
|
|
|
const [loading, setLoading] = useState(false);
|
2022-05-19 09:46:26 +08:00
|
|
|
const [data, setData] = useState<DataType[]>([]);
|
2017-09-06 15:53:25 +08:00
|
|
|
|
2021-10-13 18:59:17 +08:00
|
|
|
const loadMoreData = () => {
|
|
|
|
if (loading) {
|
2017-09-25 15:24:16 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-10-13 18:59:17 +08:00
|
|
|
setLoading(true);
|
2021-10-14 12:09:07 +08:00
|
|
|
fetch('https://randomuser.me/api/?results=10&inc=name,gender,email,nat,picture&noinfo')
|
2021-10-13 18:59:17 +08:00
|
|
|
.then(res => res.json())
|
|
|
|
.then(body => {
|
|
|
|
setData([...data, ...body.results]);
|
|
|
|
setLoading(false);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setLoading(false);
|
2017-09-06 15:53:25 +08:00
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2021-10-13 18:59:17 +08:00
|
|
|
useEffect(() => {
|
|
|
|
loadMoreData();
|
|
|
|
}, []);
|
2017-09-06 15:53:25 +08:00
|
|
|
|
2021-10-13 18:59:17 +08:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
id="scrollableDiv"
|
|
|
|
style={{
|
|
|
|
height: 400,
|
|
|
|
overflow: 'auto',
|
|
|
|
padding: '0 16px',
|
|
|
|
border: '1px solid rgba(140, 140, 140, 0.35)',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<InfiniteScroll
|
|
|
|
dataLength={data.length}
|
|
|
|
next={loadMoreData}
|
|
|
|
hasMore={data.length < 50}
|
|
|
|
loader={<Skeleton avatar paragraph={{ rows: 1 }} active />}
|
|
|
|
endMessage={<Divider plain>It is all, nothing more 🤐</Divider>}
|
|
|
|
scrollableTarget="scrollableDiv"
|
|
|
|
>
|
|
|
|
<List
|
|
|
|
dataSource={data}
|
|
|
|
renderItem={item => (
|
2022-05-19 09:46:26 +08:00
|
|
|
<List.Item key={item.email}>
|
2021-10-13 18:59:17 +08:00
|
|
|
<List.Item.Meta
|
2021-10-14 12:09:07 +08:00
|
|
|
avatar={<Avatar src={item.picture.large} />}
|
2021-10-13 18:59:17 +08:00
|
|
|
title={<a href="https://ant.design">{item.name.last}</a>}
|
|
|
|
description={item.email}
|
|
|
|
/>
|
|
|
|
<div>Content</div>
|
|
|
|
</List.Item>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</InfiniteScroll>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2017-10-11 10:29:45 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|