Proyectos de Subversion LeadersLinked - SPA

Rev

Autoría | Ultima modificación | Ver Log |

import { useCallback, useRef } from 'react';

export const useInfinityScroll = ({ isLoading, hasMore, onIntersect }) => {
  const observer = useRef(null);

  const lastElementRef = useCallback(
    (node) => {
      if (isLoading) return;
      if (observer.current) observer.current.disconnect();

      observer.current = new IntersectionObserver((entries) => {
        if (entries[0].isIntersecting && hasMore) {
          onIntersect();
        }
      });

      if (node) observer.current.observe(node);
    },
    [isLoading, hasMore, onIntersect]
  );

  return { lastElementRef };
};