From e57e5d6ad90d89a0ae93a3b3eadcd8fcac20fea0 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 21 Aug 2022 15:12:50 +0800 Subject: [PATCH] refactor(use-intersection): remove useRef usage --- packages/next/client/use-intersection.tsx | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/packages/next/client/use-intersection.tsx b/packages/next/client/use-intersection.tsx index 8d3ecd1bece5..0474d7bad594 100644 --- a/packages/next/client/use-intersection.tsx +++ b/packages/next/client/use-intersection.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useRef, useState } from 'react' +import { useCallback, useEffect, useState } from 'react' import { requestIdleCallback, cancelIdleCallback, @@ -100,30 +100,21 @@ export function useIntersection({ }: UseIntersection): [(element: T | null) => void, boolean, () => void] { const isDisabled: boolean = disabled || !hasIntersectionObserver - const unobserve = useRef() const [visible, setVisible] = useState(false) const [element, setElement] = useState(null) useEffect(() => { if (hasIntersectionObserver) { - if (unobserve.current) { - unobserve.current() - unobserve.current = undefined - } - if (isDisabled || visible) return if (element && element.tagName) { - unobserve.current = observe( + const unobserve = observe( element, (isVisible) => isVisible && setVisible(isVisible), { root: rootRef?.current, rootMargin } ) - } - return () => { - unobserve.current?.() - unobserve.current = undefined + return unobserve } } else { if (!visible) {