diff --git a/src/List.js b/src/List.js index 8b53314..90d89b6 100644 --- a/src/List.js +++ b/src/List.js @@ -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); }, []); @@ -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)])); diff --git a/src/useInfiniteScroll.js b/src/useInfiniteScroll.js index cfb3729..0ba981d 100644 --- a/src/useInfiniteScroll.js +++ b/src/useInfiniteScroll.js @@ -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); }, []); @@ -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]; };