From 13bb87c0df7557ff796fe0bb2051cfe7aa9b93c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gide=C3=A3o?= Date: Tue, 22 Apr 2025 09:34:02 -0300 Subject: [PATCH 1/2] check if it is active --- src/VisibilitySensor.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/VisibilitySensor.tsx b/src/VisibilitySensor.tsx index 1ccdae4..818feec 100644 --- a/src/VisibilitySensor.tsx +++ b/src/VisibilitySensor.tsx @@ -114,6 +114,8 @@ const VisibilitySensor = forwardRef( }, [disabled, startWatching, stopWatching]); useEffect(() => { + if (!active) return; + const window: ScaledSize = Dimensions.get('window'); const isVisible: boolean = rectDimensions.rectTop + (threshold.top || 0) <= window.height && // Top edge is within the bottom of the window From d60a41188a44d48ce038b205252ba5880d30e509 Mon Sep 17 00:00:00 2001 From: Jairaj Jangle <25704330+JairajJangle@users.noreply.github.com> Date: Sat, 26 Apr 2025 11:06:38 +0530 Subject: [PATCH 2/2] fix: incorrect visibility state on initial render - fixes #30 Updates PR #33 to add more checks and added missing state in useEffect's dependency array --- src/VisibilitySensor.tsx | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/VisibilitySensor.tsx b/src/VisibilitySensor.tsx index 818feec..2d48b72 100644 --- a/src/VisibilitySensor.tsx +++ b/src/VisibilitySensor.tsx @@ -58,8 +58,11 @@ const VisibilitySensor = forwardRef( }); const [lastValue, setLastValue] = useState(undefined); const [active, setActive] = useState(false); + const hasMeasuredRef = useRef(false); const measureInnerView = () => { + /* Check if the sensor is active to prevent unnecessary measurements + This avoids running measurements when the sensor is disabled or stopped */ if (!active) return; localRef.current?.measure( @@ -88,6 +91,9 @@ const VisibilitySensor = forwardRef( rectDimensions.rectHeight !== dimensions.rectHeight ) { setRectDimensions(dimensions); + /* Set hasMeasuredRef to true to indicate that a valid measurement has been taken + This ensures visibility checks only proceed after initial measurement */ + hasMeasuredRef.current = true; } } ); @@ -100,7 +106,12 @@ const VisibilitySensor = forwardRef( }, [active]); const stopWatching = useCallback(() => { - if (active) setActive(false); + if (active) { + setActive(false); + /* Reset measurement state when stopping to ensure fresh measurements + when the sensor is reactivated */ + hasMeasuredRef.current = false; + } }, [active]); useEffect(() => { @@ -114,7 +125,10 @@ const VisibilitySensor = forwardRef( }, [disabled, startWatching, stopWatching]); useEffect(() => { - if (!active) return; + /* Ensure visibility checks only run when the sensor is active and + at least one measurement has been completed. This prevents + premature visibility calculations with invalid or stale dimensions */ + if (!active || !hasMeasuredRef.current) return; const window: ScaledSize = Dimensions.get('window'); const isVisible: boolean = @@ -131,7 +145,7 @@ const VisibilitySensor = forwardRef( } } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [rectDimensions, lastValue]); + }, [rectDimensions, lastValue, active]); return (