From f4c54616d99a382072fba76eaf5d37752871ac4a Mon Sep 17 00:00:00 2001 From: elbica Date: Wed, 17 May 2023 16:17:59 +0900 Subject: [PATCH] =?UTF-8?q?:bug:=20Fix:=20=EB=B0=88=20api=20=EB=AC=B4?= =?UTF-8?q?=ED=95=9C=EC=9A=94=EC=B2=AD=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/application/hooks/common/useIntersect.ts | 21 +++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/application/hooks/common/useIntersect.ts b/src/application/hooks/common/useIntersect.ts index 65a02040..92cc72b7 100644 --- a/src/application/hooks/common/useIntersect.ts +++ b/src/application/hooks/common/useIntersect.ts @@ -4,20 +4,23 @@ type IntersectHandler = (entry: IntersectionObserverEntry, observer: Intersectio export const useIntersect = (onIntersect: IntersectHandler, options?: IntersectionObserverInit) => { const [ref, setRef] = useState(null); - const callbackRef = useRef( - (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => { - entries.forEach((entry) => { - if (entry.isIntersecting) onIntersect(entry, observer); - }); - }, + const callback = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => { + entries.forEach((entry) => { + if (entry.isIntersecting) onIntersect(entry, observer); + }); + }; + const observerRef = useRef( + typeof IntersectionObserver === "undefined" + ? undefined + : new IntersectionObserver(callback, options), ); useEffect(() => { - if (!ref) return; - const observer = new IntersectionObserver(callbackRef.current, options); + const observer = observerRef.current; + if (!ref || !observer) return; observer.observe(ref); return () => observer.disconnect(); - }, [ref, options]); + }, [ref]); return setRef; };