diff --git a/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js b/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js index 30a56756d46..1783adbc167 100644 --- a/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js +++ b/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserver.js @@ -141,12 +141,14 @@ export default class IntersectionObserver { return; } - IntersectionObserverManager.observe({ + const didStartObserving = IntersectionObserverManager.observe({ intersectionObserverId: this._getOrCreateIntersectionObserverId(), target, }); - this._observationTargets.add(target); + if (didStartObserving) { + this._observationTargets.add(target); + } } /** diff --git a/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserverManager.js b/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserverManager.js index e15df994e35..d2d0067323c 100644 --- a/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserverManager.js +++ b/packages/react-native/src/private/webapis/intersectionobserver/IntersectionObserverManager.js @@ -116,10 +116,10 @@ export function observe({ }: { intersectionObserverId: IntersectionObserverId, target: ReactNativeElement, -}): void { +}): boolean { if (NativeIntersectionObserver == null) { warnNoNativeIntersectionObserver(); - return; + return false; } const registeredObserver = registeredIntersectionObservers.get( @@ -129,15 +129,13 @@ export function observe({ console.error( `IntersectionObserverManager: could not start observing target because IntersectionObserver with ID ${intersectionObserverId} was not registered.`, ); - return; + return false; } const targetShadowNode = getShadowNode(target); if (targetShadowNode == null) { - console.error( - 'IntersectionObserverManager: could not find reference to host node from target', - ); - return; + // The target is disconnected. We can't observe it anymore. + return false; } const instanceHandle = getInstanceHandle(target); @@ -145,7 +143,7 @@ export function observe({ console.error( 'IntersectionObserverManager: could not find reference to instance handle from target', ); - return; + return false; } // Store the mapping between the instance handle and the target so we can @@ -160,11 +158,13 @@ export function observe({ isConnected = true; } - return NativeIntersectionObserver.observe({ + NativeIntersectionObserver.observe({ intersectionObserverId, targetShadowNode, thresholds: registeredObserver.observer.thresholds, }); + + return true; } export function unobserve(