mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-16 01:41:02 +08:00
827225b895
* docs: replace avatar image to joeschmoe placeholder * docs: usage randomme avatar url
83 lines
2.1 KiB
Markdown
83 lines
2.1 KiB
Markdown
---
|
|
order: 6
|
|
title:
|
|
zh-CN: 滚动加载
|
|
en-US: Scrolling loaded
|
|
---
|
|
|
|
## zh-CN
|
|
|
|
结合 [react-infinite-scroll-component](https://github.com/ankeetmaini/react-infinite-scroll-component) 实现滚动自动加载列表。
|
|
|
|
## en-US
|
|
|
|
The example of infinite load with [react-infinite-scroll-component](https://github.com/ankeetmaini/react-infinite-scroll-component).
|
|
|
|
```jsx
|
|
import React, { useState, useEffect } from 'react';
|
|
import { List, message, Avatar, Skeleton, Divider } from 'antd';
|
|
import InfiniteScroll from 'react-infinite-scroll-component';
|
|
|
|
const InfiniteListExample = () => {
|
|
const [loading, setLoading] = useState(false);
|
|
const [data, setData] = useState([]);
|
|
|
|
const loadMoreData = () => {
|
|
if (loading) {
|
|
return;
|
|
}
|
|
setLoading(true);
|
|
fetch('https://randomuser.me/api/?results=10&inc=name,gender,email,nat,picture&noinfo')
|
|
.then(res => res.json())
|
|
.then(body => {
|
|
setData([...data, ...body.results]);
|
|
setLoading(false);
|
|
})
|
|
.catch(() => {
|
|
setLoading(false);
|
|
});
|
|
};
|
|
|
|
useEffect(() => {
|
|
loadMoreData();
|
|
}, []);
|
|
|
|
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 => (
|
|
<List.Item key={item.id}>
|
|
<List.Item.Meta
|
|
avatar={<Avatar src={item.picture.large} />}
|
|
title={<a href="https://ant.design">{item.name.last}</a>}
|
|
description={item.email}
|
|
/>
|
|
<div>Content</div>
|
|
</List.Item>
|
|
)}
|
|
/>
|
|
</InfiniteScroll>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
ReactDOM.render(<InfiniteListExample />, mountNode);
|
|
```
|