Skip to content

Commit

Permalink
update infinity scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
edelgarat committed Jul 3, 2024
1 parent 4f60016 commit c2e14ff
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@worksolutions/react-utils",
"private": false,
"version": "2.2.4",
"version": "2.2.5",
"description": "",
"types": "dist/esm/index.d.ts",
"main": "dist/cjs/index.js",
Expand Down
30 changes: 25 additions & 5 deletions src/hooks/useInfinityScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { useSyncToRef } from "./useSyncToRef";
type UseInfiniteScrollDirection = "down" | "up";

interface UseInfinityScrollConfig {
loading: boolean;
loading?: boolean;
loadingRef?: React.MutableRefObject<boolean>;
waitLoadingMS?: number;
hasNextPage: boolean;
threshold?: number;
startObservingDelay?: number;
Expand Down Expand Up @@ -36,14 +38,21 @@ const scrollRemainderDetectors = {
export function useInfinityScroll({
scrollCheckInterval = 200,
threshold = 200,
loading,
waitLoadingMS,
loading: incomeLoading,
loadingRef: incomeLoadingRef,
startObservingDelay = 0,
direction = "down",
hasNextPage,
onLoadMore,
}: UseInfinityScrollConfig) {
const [scrollableElement, setScrollableElement] = React.useState<ScrollableElement>(null);
const loadingRef = useSyncToRef(loading);
const [scrollableElement, setScrollableElement] = React.useState<ScrollableElement | null>(null);

const loadingRef1 = incomeLoadingRef;
const loadingRef2 = useSyncToRef(incomeLoading);

const loadingRef = useSyncToRef(loadingRef1?.current ?? loadingRef2.current ?? false);

const hasNextPageRef = useSyncToRef(hasNextPage);
const onLoadMoreRef = useSyncToRef(onLoadMore);

Expand All @@ -53,6 +62,7 @@ export function useInfinityScroll({
startObservingDelay,
scrollCheckInterval,
direction,
waitLoadingMS,
hasNextPage: hasNextPageRef,
loading: loadingRef,
onLoadMore: onLoadMoreRef,
Expand All @@ -73,6 +83,7 @@ function useScrollListener({
scrollableElement,
hasNextPage,
loading,
waitLoadingMS,
threshold,
direction,
onLoadMore,
Expand All @@ -86,16 +97,24 @@ function useScrollListener({
direction: UseInfiniteScrollDirection;
hasNextPage: React.MutableRefObject<boolean>;
loading: React.MutableRefObject<boolean>;
waitLoadingMS: number | undefined;
onLoadMore: React.MutableRefObject<() => void>;
}) {
React.useEffect(() => {
if (!scrollableElement) return () => null;

let waitLoadingTimer: NodeJS.Timeout | null = null;

const listener = throttle(function () {
if (!hasNextPage.current || loading.current) return;
const scrollToEnd = scrollRemainderDetectors[direction](scrollableElement);
if (scrollToEnd > threshold) return;
onLoadMore.current();
if (waitLoadingMS === undefined) return onLoadMore.current();
clearTimeout(waitLoadingTimer!);
waitLoadingTimer = setTimeout(() => {
if (loading.current) return;
onLoadMore.current();
}, waitLoadingMS);
}, scrollCheckInterval);

const startObservingTimeout = setTimeout(
Expand All @@ -116,6 +135,7 @@ function useScrollListener({
scrollCheckInterval,
scrollableElement,
threshold,
waitLoadingMS,
]);
}

Expand Down
4 changes: 1 addition & 3 deletions src/hooks/useSyncToRef.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React from "react";

import { useEffectSkipFirst } from "./common";

export function useSyncToRef<T>(data: T) {
const ref = React.useRef(data);
useEffectSkipFirst(() => {
React.useMemo(() => {
ref.current = data;
}, [data]);
return ref;
Expand Down

0 comments on commit c2e14ff

Please sign in to comment.