Last active
January 4, 2021 10:08
-
-
Save markninja96/d66427fe9178cffae006e6bd9c6715a7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //useInfiniteScroll.js | |
| import { useRef, useCallback } from "react"; | |
| const useInfiniteScroll = (callback, isFetching) => { | |
| //here we use useRef to store a DOM node and the returned object will persist regardless of re-renders | |
| const observer = useRef(); | |
| //useCallback takes a callback argument and an array dependency list and returns a memoized callback | |
| //which is guaranteed to have the same reference | |
| const lastElementRef = useCallback( | |
| (node) => { | |
| if (isFetching) return; | |
| //stop watching targets, you can think of it as a reset | |
| if (observer.current) observer.current.disconnect(); | |
| //create a new intersection observer and execute the callback incase of an intersecting event | |
| observer.current = new IntersectionObserver((entries) => { | |
| if (entries[0].isIntersecting) { | |
| callback(); | |
| } | |
| }); | |
| //if there is a node, let the intersection observer watch that node | |
| if (node) observer.current.observe(node); | |
| }, | |
| [callback, isFetching] | |
| ); | |
| //return reference to the last element | |
| return [lastElementRef]; | |
| }; | |
| export default useInfiniteScroll; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment