From 89085d506910c601925864570e2d2e1feae0dd0e Mon Sep 17 00:00:00 2001 From: pramod mahajan Date: Fri, 18 Jul 2025 15:01:44 +0530 Subject: [PATCH] prevent IntersectionObserver error on unobserve with null ref --- src/pages/Gallary/ImageGallary.jsx | 39 ++++++++++++++++-------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/pages/Gallary/ImageGallary.jsx b/src/pages/Gallary/ImageGallary.jsx index dfa9e7cb..16335df2 100644 --- a/src/pages/Gallary/ImageGallary.jsx +++ b/src/pages/Gallary/ImageGallary.jsx @@ -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(