Skip to content

Commit 91ccdd0

Browse files
authored
Pass reanimatedEventHandler only when the Reanimated detector is used (#4429)
## Description `NativeDetector` always passes `reanimatedEventHandler` to the native component, even when the gesture does not use the Reanimated detector. Two different conditions decide whether the handler exists and whether the Reanimated detector is used. `useGestureCallbacks` builds the handler whenever `disableReanimated` is unset: ```ts // src/v3/hooks/useGestureCallbacks.ts let reanimatedEventHandler; if (!config.disableReanimated) { ``` `shouldUseReanimatedDetector` additionally requires worklet callbacks: ```ts // src/v3/hooks/utils/configUtils.ts config.shouldUseReanimatedDetector = !config.disableReanimated && Reanimated !== undefined && hasWorkletEventHandlers(config) && !config.dispatchesAnimatedEvents; ``` So a gesture with no worklet callbacks gets `shouldUseReanimatedDetector === false`, renders the plain `HostGestureDetector`, which forwards props verbatim, and receives Reanimated's `{ workletEventHandler }` object under `onGestureHandlerReanimatedEvent`. That prop is a codegen `DirectEventHandler`, so React throws out of `getListener` instead of dispatching: ``` Expected `onGestureHandlerReanimatedEvent` listener to be a function, instead got a value of `object` type. ``` It stays latent most of the time, because a handler on a plain detector gets `dispatchesReanimatedEvents: shouldUseReanimatedDetector && !runOnJS`, which is false, so it never emits the event. It surfaces once a handler ends up attached to a detector that is not its own. The prop is invalid either way. This passes the handler only when the Reanimated detector is actually used. The event prop is part of the static view config, so `undefined` only means there is no JS listener, and native still emits the event. I left the web branch untouched, since I have not tested whether the same mismatch applies there. Happy to extend it if you think it does. Fixes #4428 ## Test plan - `yarn ts-check`, `yarn lint:js` and `yarn test` in `packages/react-native-gesture-handler`. - Repro app: https://github.com/antFrancon/rngh-reanimated-handler-repro. It renders four gestures and prints, for each, which detector `NativeDetector` picks and the `typeof` of the handler it receives. - Before: `useNativeGesture()`, `usePanGesture()` and `useTapGesture({ onActivate })` all render `HostGestureDetector` with `typeof reanimatedEventHandler === 'object'`. - After: those three receive `undefined`, and the worklet control still gets the handler on `ReanimatedNativeDetector`. - We have been running this change as a patch in an app on Expo SDK 57, RN 0.86.2, React 19.2.3, Reanimated 4.5.3, Fabric, Hermes, iOS, with no regressions in gesture behaviour.
1 parent b2d23f1 commit 91ccdd0

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

‎packages/react-native-gesture-handler/src/v3/detectors/NativeDetector.tsx‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,24 @@ export function NativeDetector<
5151
// On native, Reanimated handles routing internally based on the event names
5252
// passed to the useEvent hook. We only need to pass it once, so that Reanimated
5353
// can setup its internal listeners.
54+
//
55+
// `reanimatedEventHandler` is built whenever `disableReanimated` is unset, but
56+
// `shouldUseReanimatedDetector` additionally requires worklet callbacks. When it is false we
57+
// render the plain host component, which forwards props verbatim, so passing the handler would
58+
// put a non-function on a codegen `DirectEventHandler` prop.
59+
const reanimatedEventHandler = gesture.config.shouldUseReanimatedDetector
60+
? gesture.detectorCallbacks.reanimatedEventHandler
61+
: undefined;
62+
5463
const reanimatedHandlers =
5564
Platform.OS === 'web'
5665
? {
57-
onGestureHandlerReanimatedEvent:
58-
gesture.detectorCallbacks.reanimatedEventHandler,
59-
onGestureHandlerReanimatedStateChange:
60-
gesture.detectorCallbacks.reanimatedEventHandler,
61-
onGestureHandlerReanimatedTouchEvent:
62-
gesture.detectorCallbacks.reanimatedEventHandler,
66+
onGestureHandlerReanimatedEvent: reanimatedEventHandler,
67+
onGestureHandlerReanimatedStateChange: reanimatedEventHandler,
68+
onGestureHandlerReanimatedTouchEvent: reanimatedEventHandler,
6369
}
6470
: {
65-
onGestureHandlerReanimatedEvent:
66-
gesture.detectorCallbacks.reanimatedEventHandler,
71+
onGestureHandlerReanimatedEvent: reanimatedEventHandler,
6772
};
6873

6974
return (

0 commit comments

Comments
 (0)