Created
March 22, 2023 14:08
-
-
Save louis-young/29d6bc797d0b0eeee12878aa06efeb08 to your computer and use it in GitHub Desktop.
Infinite Scroll
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
import { useRef } from "react"; | |
import { useIntersectionObserver } from "../../hooks/useIntersectionObserver"; | |
import type { InfiniteScrollProps } from "./types"; | |
export const InfiniteScroll = ({ | |
loadMoreFunction, | |
isLoadingMore, | |
loadingMoreMessage, | |
hasLoadedEverything, | |
loadedEverythingMessage, | |
children, | |
}: InfiniteScrollProps) => { | |
const observableRef = useRef<HTMLDivElement>(null); | |
useIntersectionObserver({ | |
observableRef, | |
callback: loadMoreFunction, | |
}); | |
return ( | |
<> | |
{children} | |
{isLoadingMore && loadingMoreMessage} | |
{hasLoadedEverything && loadedEverythingMessage} | |
<div ref={observableRef} /> | |
</> | |
); | |
}; |
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
import type { ReactNode } from "react"; | |
export interface InfiniteScrollProps { | |
loadMoreFunction: () => void; | |
isLoadingMore: boolean; | |
loadingMoreMessage: ReactNode; | |
hasLoadedEverything: boolean; | |
loadedEverythingMessage: ReactNode; | |
children: ReactNode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment