From 4d4660d7f7709f15677c87021f605793c9b9adc3 Mon Sep 17 00:00:00 2001 From: Jakub Kosmydel Date: Wed, 17 Jun 2026 14:10:56 +0200 Subject: [PATCH] [General] Register mount reactions in layout effect to avoid crash on unmounted detector Run the GestureDetector mount listener in a layout effect (matching the attach/drop phase) and bail out when the detector is no longer mounted, preventing updates to a detached detector. Co-authored-by: Cursor --- .../gestures/GestureDetector/useMountReactions.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/useMountReactions.ts b/packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/useMountReactions.ts index 84732ddbb1..4318eba15d 100644 --- a/packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/useMountReactions.ts +++ b/packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/useMountReactions.ts @@ -1,6 +1,5 @@ -import { useEffect } from 'react'; - import { MountRegistry } from '../../../mountRegistry'; +import { useIsomorphicLayoutEffect } from '../../../useIsomorphicLayoutEffect'; import { transformIntoHandlerTags } from '../../utils'; import type { GestureRef } from '../gesture'; import type { AttachedGestureState } from './types'; @@ -26,8 +25,14 @@ export function useMountReactions( updateDetector: () => void, state: AttachedGestureState ) { - useEffect(() => { + useIsomorphicLayoutEffect(() => { return MountRegistry.addMountListener((gesture) => { + // The detector may already be unmounted when this fires; bail out to avoid + // updating a detached detector. + if (!state.isMounted) { + return; + } + // At this point the ref in the gesture config should be updated, so we can check if one of the gestures // set in a relation with the gesture got mounted. If so, we need to update the detector to propagate // the changes to the native side.