Skip to content

Commit ad2029d

Browse files
authored
Throw error when detectors are not placed inside GestureHandlerRootView (#3798)
## Description For now, `NativeDetector` doesn't throw error when it is used outside of `GestureHandlerRootView`. This PR moves check from `LegacyDetector` to `GestureDetector`. ## Test plan <details> <summary>Tested on the following example:</summary> ```tsx import React from 'react'; import { StyleSheet, View } from 'react-native'; import { usePan, GestureDetector } from 'react-native-gesture-handler'; export default function EmptyExample() { const pan = usePan({}); return ( <View style={styles.container}> <GestureDetector gesture={pan}> <View style={{ width: 300, height: 300, backgroundColor: 'green' }} /> </GestureDetector> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, }); ``` </details>
1 parent 76caf44 commit ad2029d

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/index.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
/* eslint-disable react/no-unused-prop-types */
2-
import React, { useContext, useEffect, useMemo, useRef } from 'react';
3-
import { Platform } from 'react-native';
2+
import React, { useEffect, useMemo, useRef } from 'react';
43
import findNodeHandle from '../../../findNodeHandle';
54
import { GestureType } from '../gesture';
65
import { UserSelect, TouchAction } from '../../gestureHandlerCommon';
76
import { ComposedGesture } from '../gestureComposition';
8-
import { isTestEnv } from '../../../utils';
9-
10-
import GestureHandlerRootViewContext from '../../../GestureHandlerRootViewContext';
117
import { AttachedGestureState, GestureDetectorState } from './types';
128
import { useAnimatedGesture } from './useAnimatedGesture';
139
import { attachHandlers } from './attachHandlers';
@@ -87,13 +83,6 @@ export interface GestureDetectorProps {
8783
* @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/gesture-detector
8884
*/
8985
export const GestureDetector = (props: GestureDetectorProps) => {
90-
const rootViewContext = useContext(GestureHandlerRootViewContext);
91-
if (__DEV__ && !rootViewContext && !isTestEnv() && Platform.OS !== 'web') {
92-
throw new Error(
93-
'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.'
94-
);
95-
}
96-
9786
// Gesture config should be wrapped with useMemo to prevent unnecessary re-renders
9887
const gestureConfig = props.gesture;
9988
propagateDetectorConfig(props, gestureConfig);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,21 @@ import {
99
import { DetectorContext } from './VirtualDetector/useDetectorContext';
1010
import { VirtualDetector } from './VirtualDetector/VirtualDetector';
1111
import { use } from 'react';
12+
import { isTestEnv } from '../../utils';
13+
import { Platform } from 'react-native';
14+
import GestureHandlerRootViewContext from '../../GestureHandlerRootViewContext';
1215

1316
export function GestureDetector<THandlerData, TConfig>(
1417
props: NativeDetectorProps<THandlerData, TConfig> | LegacyGestureDetectorProps
1518
) {
19+
const rootViewContext = use(GestureHandlerRootViewContext);
20+
21+
if (__DEV__ && !rootViewContext && !isTestEnv() && Platform.OS !== 'web') {
22+
throw new Error(
23+
'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.'
24+
);
25+
}
26+
1627
if (
1728
props.gesture instanceof ComposedGesture ||
1829
props.gesture instanceof BaseGesture

0 commit comments

Comments
 (0)