From ee482a331e353740659ffc5286285bb81d37fee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Mon, 3 Nov 2025 11:57:11 +0100 Subject: [PATCH] Check for RootView --- .../src/handlers/gestures/GestureDetector/index.tsx | 13 +------------ .../src/v3/detectors/GestureDetector.tsx | 11 +++++++++++ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/index.tsx b/packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/index.tsx index 12ff60d1cb..e1a46e1b1a 100644 --- a/packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/index.tsx +++ b/packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/index.tsx @@ -1,13 +1,9 @@ /* eslint-disable react/no-unused-prop-types */ -import React, { useContext, useEffect, useMemo, useRef } from 'react'; -import { Platform } from 'react-native'; +import React, { useEffect, useMemo, useRef } from 'react'; import findNodeHandle from '../../../findNodeHandle'; import { GestureType } from '../gesture'; import { UserSelect, TouchAction } from '../../gestureHandlerCommon'; import { ComposedGesture } from '../gestureComposition'; -import { isTestEnv } from '../../../utils'; - -import GestureHandlerRootViewContext from '../../../GestureHandlerRootViewContext'; import { AttachedGestureState, GestureDetectorState } from './types'; import { useAnimatedGesture } from './useAnimatedGesture'; import { attachHandlers } from './attachHandlers'; @@ -87,13 +83,6 @@ export interface GestureDetectorProps { * @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/gesture-detector */ export const GestureDetector = (props: GestureDetectorProps) => { - const rootViewContext = useContext(GestureHandlerRootViewContext); - if (__DEV__ && !rootViewContext && !isTestEnv() && Platform.OS !== 'web') { - throw new Error( - 'GestureDetector must be used as a descendant of GestureHandlerRootView. Otherwise the gestures will not be recognized. See https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/installation for more details.' - ); - } - // Gesture config should be wrapped with useMemo to prevent unnecessary re-renders const gestureConfig = props.gesture; propagateDetectorConfig(props, gestureConfig); diff --git a/packages/react-native-gesture-handler/src/v3/detectors/GestureDetector.tsx b/packages/react-native-gesture-handler/src/v3/detectors/GestureDetector.tsx index ba4d468a11..36a891693a 100644 --- a/packages/react-native-gesture-handler/src/v3/detectors/GestureDetector.tsx +++ b/packages/react-native-gesture-handler/src/v3/detectors/GestureDetector.tsx @@ -9,10 +9,21 @@ import { import { DetectorContext } from './LogicDetector/useDetectorContext'; import { LogicDetector } from './LogicDetector/LogicDetector'; import { use } from 'react'; +import { isTestEnv } from '../../utils'; +import { Platform } from 'react-native'; +import GestureHandlerRootViewContext from '../../GestureHandlerRootViewContext'; export function GestureDetector( props: NativeDetectorProps | LegacyGestureDetectorProps ) { + const rootViewContext = use(GestureHandlerRootViewContext); + + if (__DEV__ && !rootViewContext && !isTestEnv() && Platform.OS !== 'web') { + throw new Error( + 'GestureDetector must be used as a descendant of GestureHandlerRootView. Otherwise the gestures will not be recognized. See https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/installation for more details.' + ); + } + if ( props.gesture instanceof ComposedGesture || props.gesture instanceof BaseGesture