import React, { useEffect, useState } from 'react'; import { Avatar, List, message } from 'antd'; import VirtualList from 'rc-virtual-list'; interface UserItem { email: string; gender: string; name: { first: string; last: string; title: string; }; nat: string; picture: { large: string; medium: string; thumbnail: string; }; } const fakeDataUrl = 'https://randomuser.me/api/?results=20&inc=name,gender,email,nat,picture&noinfo'; const ContainerHeight = 400; const App: React.FC = () => { const [data, setData] = useState([]); const appendData = (showMessage = true) => { fetch(fakeDataUrl) .then((res) => res.json()) .then((body) => { setData(data.concat(body.results)); showMessage && message.success(`${body.results.length} more items loaded!`); }); }; useEffect(() => { appendData(false); }, []); const onScroll = (e: React.UIEvent) => { // Refer to: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#problems_and_solutions if (Math.abs(e.currentTarget.scrollHeight - e.currentTarget.scrollTop - ContainerHeight) <= 1) { appendData(); } }; return ( {(item: UserItem) => ( } title={{item.name.last}} description={item.email} />
Content
)}
); }; export default App;