Skip to content

Instantly share code, notes, and snippets.

@louis-young
Created March 22, 2023 14:08
Show Gist options
  • Save louis-young/29d6bc797d0b0eeee12878aa06efeb08 to your computer and use it in GitHub Desktop.
Save louis-young/29d6bc797d0b0eeee12878aa06efeb08 to your computer and use it in GitHub Desktop.
Infinite Scroll
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} />
</>
);
};
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