Skip to content

Commit 0f3115f

Browse files
kosmydelcursoragent
andcommitted
[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 <cursoragent@cursor.com>
1 parent 7453280 commit 0f3115f

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/useMountReactions.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { useEffect } from 'react';
2-
31
import { MountRegistry } from '../../../mountRegistry';
42
import { transformIntoHandlerTags } from '../../utils';
53
import type { GestureRef } from '../gesture';
@@ -28,6 +26,12 @@ export function useMountReactions(
2826
) {
2927
useEffect(() => {
3028
return MountRegistry.addMountListener((gesture) => {
29+
// The detector may already be unmounted when this fires; bail out to avoid
30+
// updating a detached detector.
31+
if (!state.isMounted) {
32+
return;
33+
}
34+
3135
// At this point the ref in the gesture config should be updated, so we can check if one of the gestures
3236
// set in a relation with the gesture got mounted. If so, we need to update the detector to propagate
3337
// the changes to the native side.
@@ -50,3 +54,7 @@ export function useMountReactions(
5054
});
5155
}, [updateDetector, state]);
5256
}
57+
function useEffect(arg0: () => () => void, arg1: ((() => void) | AttachedGestureState)[]) {
58+
throw new Error('Function not implemented.');
59+
}
60+

0 commit comments

Comments
 (0)