Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const List = () => {
const [isFetching, setIsFetching] = useState(false);

useEffect(() => {
function handleScroll() {
if (Math.ceil(window.innerHeight + document.documentElement.scrollTop) !== document.documentElement.offsetHeight || isFetching) return;
setIsFetching(true);
}
window.addEventListener('scroll', handleScroll);
return () => window.removeEventListener('scroll', handleScroll);
}, []);
Expand All @@ -14,11 +18,6 @@ const List = () => {
fetchMoreListItems();
}, [isFetching]);

function handleScroll() {
if (window.innerHeight + document.documentElement.scrollTop !== document.documentElement.offsetHeight || isFetching) return;
setIsFetching(true);
}

function fetchMoreListItems() {
setTimeout(() => {
setListItems(prevState => ([...prevState, ...Array.from(Array(20).keys(), n => n + prevState.length + 1)]));
Expand Down
9 changes: 4 additions & 5 deletions src/useInfiniteScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const useInfiniteScroll = (callback) => {
const [isFetching, setIsFetching] = useState(false);

useEffect(() => {
function handleScroll() {
if (Math.ceil(window.innerHeight + document.documentElement.scrollTop) !== document.documentElement.offsetHeight || isFetching) return;
setIsFetching(true);
}
window.addEventListener('scroll', handleScroll);
return () => window.removeEventListener('scroll', handleScroll);
}, []);
Expand All @@ -13,11 +17,6 @@ const useInfiniteScroll = (callback) => {
callback();
}, [isFetching]);

function handleScroll() {
if (window.innerHeight + document.documentElement.scrollTop !== document.documentElement.offsetHeight || isFetching) return;
setIsFetching(true);
}

return [isFetching, setIsFetching];
};

Expand Down