Skip to content

Commit 991ccb5

Browse files
authored
fix: crash when mount listener fires after GestureDetector unmount (#4268)
## Description `useMountReactions` registers a `MountRegistry` listener in `useEffect`. That listener can fire after the owning `GestureDetector` has already unmounted (e.g. a related gesture mounts while this detector is gone). Calling `updateDetector` in that case updates a detached detector and can crash (occasionally in `findNodeHandle`). Guard with the existing `state.isMounted` flag — the same pattern `attachHandlers` already uses for the microtask after unmount. Credit to @hannojg for the original investigation/fix direction. ## Changes - In `useMountReactions`, return early from the mount listener when `!state.isMounted`. ## Why not `useLayoutEffect`? An earlier version of this PR also moved the subscription to `useIsomorphicLayoutEffect` (same phase as GestureDetector attach/drop). We A/B tested `useLayoutEffect` vs stock `useEffect`, both with this early return. Crash rates were equivalent; the `isMounted` check alone is sufficient, so this PR keeps `useEffect`.
1 parent bc605eb commit 991ccb5

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export function useMountReactions(
2828
) {
2929
useEffect(() => {
3030
return MountRegistry.addMountListener((gesture) => {
31+
// The detector may already be unmounted when this fires; bail out to avoid
32+
// updating a detached detector.
33+
if (!state.isMounted) {
34+
return;
35+
}
36+
3137
// At this point the ref in the gesture config should be updated, so we can check if one of the gestures
3238
// set in a relation with the gesture got mounted. If so, we need to update the detector to propagate
3339
// the changes to the native side.

0 commit comments

Comments
 (0)