prevent IntersectionObserver error on unobserve with null ref

This commit is contained in:
pramod mahajan 2025-07-18 15:01:44 +05:30
parent 425c32750c
commit 89085d5069

View File

@ -106,24 +106,27 @@ const ImageGallery = () => {
return () => eventBus.off("image_gallery", handler);
}, [selectedProjectId, refetch]);
useEffect(() => {
if (!loaderRef.current) return;
const obs = new IntersectionObserver(
([entry]) => {
if (
entry.isIntersecting &&
hasNextPage &&
!isFetchingNextPage &&
!isLoading
) {
fetchNextPage();
}
},
{ rootMargin: "200px", threshold: 0.1 }
);
obs.observe(loaderRef.current);
return () => obs.unobserve(loaderRef.current);
}, [hasNextPage, isFetchingNextPage, isLoading, fetchNextPage]);
useEffect(() => {
if (!loaderRef.current) return;
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting && hasNextPage && !isFetchingNextPage && !isLoading) {
fetchNextPage();
}
},
{ rootMargin: "200px", threshold: 0.1 }
);
observer.observe(loaderRef.current);
return () => {
if (loaderRef.current) {
observer.unobserve(loaderRef.current);
}
};
}, [hasNextPage, isFetchingNextPage, isLoading, fetchNextPage]);
// Utility: derive filter options
const getUniqueValues = useCallback(