Last active
September 16, 2020 16:26
-
-
Save dabit3/e10722334551945c8ec9158eacb29f15 to your computer and use it in GitHub Desktop.
Infiinite List Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
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'; | |
const InfiniteListExample = () => { | |
let [data, setData] = useState([]) | |
const [loading, setLoading] = useState(false) | |
const [hasMore, setHasMore] = useState(true) | |
useEffect(() => { | |
fetchData(res => setData(res.results)); | |
}, []) | |
fetchData = callback => { | |
reqwest({ | |
url: fakeDataUrl, | |
type: 'json', | |
method: 'get', | |
contentType: 'application/json', | |
success: res => { | |
callback(res); | |
}, | |
}); | |
} | |
handleInfiniteOnLoad = () => { | |
setLoading(true) | |
if (data.length > 14) { | |
message.warning('Infinite List loaded all'); | |
setHasMore(false) | |
setLoading(false) | |
return; | |
} | |
fetchData(res => { | |
const newData = data.concat(res.results); | |
setData(newData) | |
setLoading(false) | |
}); | |
}; | |
return ( | |
<div className="demo-infinite-container"> | |
<InfiniteScroll | |
initialLoad={false} | |
pageStart={0} | |
loadMore={handleInfiniteOnLoad} | |
hasMore={!loading && hasMore} | |
useWindow={false} | |
> | |
<List | |
dataSource={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> | |
)} | |
> | |
{loading && hasMore && ( | |
<div className="demo-loading-container"> | |
<Spin /> | |
</div> | |
)} | |
</List> | |
</InfiniteScroll> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment