Created
December 3, 2019 00:27
-
-
Save CITGuru/67ca2f25ee92821d089838f06d7a0daa to your computer and use it in GitHub Desktop.
use infinite scroll
This file contains 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"; | |
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