Skip to content

Instantly share code, notes, and snippets.

@CITGuru
Created December 3, 2019 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CITGuru/67ca2f25ee92821d089838f06d7a0daa to your computer and use it in GitHub Desktop.
Save CITGuru/67ca2f25ee92821d089838f06d7a0daa to your computer and use it in GitHub Desktop.
use infinite scroll
import { useState, useEffect } from "react";
const useInfiniteScroll = callback => {
const [isFetching, setIsFetching] = useState(false);
useEffect(() => {
window.addEventListener("scroll", isScrolling);
return () => window.removeEventListener("scroll", isScrolling);
}, []);
useEffect(() => {
if (!isFetching) return;
callback();
}, [isFetching]);
function isScrolling() {
if (
window.innerHeight + document.documentElement.scrollTop !==
document.documentElement.offsetHeight ||
isFetching
)
return;
setIsFetching(true);
}
return [isFetching, setIsFetching];
};
export default useInfiniteScroll;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment