|
| 1 | +--- |
| 2 | +id: create-native-wrapper |
| 3 | +title: createNativeWrapper |
| 4 | +sidebar_label: createNativeWrapper |
| 5 | +--- |
| 6 | + |
| 7 | +`createNativeWrapper` is a function that lets you wrap components which are not provided by Gesture Handler with a [`Native gesture`](/docs/gestures/use-native-gesture), allowing them to participate in the gesture recognition process. |
| 8 | + |
| 9 | +```tsx |
| 10 | +import { Switch } from 'react-native'; |
| 11 | +import { createNativeWrapper } from 'react-native-gesture-handler'; |
| 12 | + |
| 13 | +const RNGHSwitch = createNativeWrapper(Switch); |
| 14 | +``` |
| 15 | + |
| 16 | +Full example can be seen in the [Example section](#example) below. |
| 17 | + |
| 18 | +This function can be useful when you want some third-party components to participate in gesture recognition process. |
| 19 | + |
| 20 | +## createNativeWrapper |
| 21 | + |
| 22 | +```ts |
| 23 | +function createNativeWrapper<P>( |
| 24 | + Component: React.ComponentType<P>, |
| 25 | + config: Readonly<NativeWrapperProperties> = {}, |
| 26 | + detectorType: GestureDetectorType = GestureDetectorType.Native, |
| 27 | +): React.FC<P & NativeWrapperProperties> |
| 28 | +``` |
| 29 | + |
| 30 | +[`config`](#config) and [`detectorType`](#detectortype) parameters are optional. Their default values are described in their respective sections below. |
| 31 | + |
| 32 | +This function returns original component wrapped with a specified [`GestureDetector`](/docs/fundamentals/gesture-detectors) that has a [`Native gesture`](/docs/gestures/use-native-gesture) attached to it: |
| 33 | + |
| 34 | +```tsx |
| 35 | +<DetectorComponent gesture={native}> |
| 36 | + <Component {...childProps} /> |
| 37 | +</DetectorComponent> |
| 38 | +``` |
| 39 | + |
| 40 | +### Component |
| 41 | + |
| 42 | +Component to be wrapped with `Native gesture`. It can be any React component, including those from third-party libraries. |
| 43 | + |
| 44 | +### config |
| 45 | + |
| 46 | +Configuration for the `Native gesture` that will be attached to the wrapped component. For more details on available options, see the `Native gesture` [configuration](/docs/gestures/use-native-gesture#config) documentation. Defaults to an empty object. |
| 47 | + |
| 48 | +### detectorType |
| 49 | + |
| 50 | +```ts |
| 51 | +enum GestureDetectorType { |
| 52 | + Native, |
| 53 | + Virtual, |
| 54 | + Intercepting, |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +Type of the gesture detector that will be used to recognize the `Native gesture`. For more details on available options, see the [Gesture Detectors](/docs/fundamentals/gesture-detectors) documentation. Defaults to `GestureDetectorType.Native` (which is just [`GestureDetector`](/docs/fundamentals/gesture-detectors#gesture-detector)). |
| 59 | + |
| 60 | +## onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER |
| 61 | + |
| 62 | +:::danger |
| 63 | +This callback may lead to infinite re-renders if not used carefully. |
| 64 | +::: |
| 65 | + |
| 66 | +```ts |
| 67 | +onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER?: (gesture: NativeGesture) => void; |
| 68 | +``` |
| 69 | + |
| 70 | +Components wrapped with `createNativeWrapper` receive an additional prop named `onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER`. This callback is triggered on every update of the `Native gesture` associated with the wrapped component, providing access to the underlying gesture. This can be helpful when setting up [relations](/docs/fundamentals/gesture-composition) with other gestures. |
| 71 | + |
| 72 | + |
| 73 | +To prevent infinite re-renders, ensure you are not updating the component with the same gesture repeatedly, e.g.: |
| 74 | + |
| 75 | +```tsx |
| 76 | +const WrappedComponent = createNativeWrapper(Component); |
| 77 | +
|
| 78 | +const ParentComponent = () => { |
| 79 | + const [nativeGesture, setNativeGesture] = useState<NativeGesture | null>( |
| 80 | + null |
| 81 | + ); |
| 82 | +
|
| 83 | + const onGestureUpdate = (gesture: NativeGesture) => { |
| 84 | + if (!nativeGesture || nativeGesture.handlerTag !== gesture.handlerTag) { |
| 85 | + setNativeGesture(gesture); |
| 86 | + ... |
| 87 | + } |
| 88 | + }; |
| 89 | +}; |
| 90 | +
|
| 91 | +<WrappedComponent |
| 92 | + ... |
| 93 | + onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER={onGestureUpdate} |
| 94 | +/>; |
| 95 | +``` |
| 96 | + |
| 97 | +You can also check example usage in our [`ScrollView` component](https://github.com/software-mansion/react-native-gesture-handler/blob/18af65aa7d1d425cecbdba3224271246ea739132/packages/react-native-gesture-handler/src/v3/components/GestureComponents.tsx#L80). |
| 98 | + |
| 99 | +## Components wrapped using createNativeWrapper |
| 100 | + |
| 101 | +Gesture Handler reexports some of the React Native components that are already wrapped using `createNativeWrapper`. These include: |
| 102 | + |
| 103 | +- `Switch` |
| 104 | +- `TextInput` |
| 105 | +- `ScrollView` |
| 106 | +- `FlatList` |
| 107 | +- `RefreshControl` |
| 108 | + |
| 109 | +[Buttons](/docs/components/buttons) are also wrapped using `createNativeWrapper` by default. |
| 110 | + |
| 111 | +## Example |
| 112 | + |
| 113 | +This example only demonstrates the usage of `createNativeWrapper`. `Switch` component from Gesture Handler comes wrapped with `createNativeWrapper` by default. |
| 114 | + |
| 115 | +```tsx |
| 116 | +import { useState } from 'react'; |
| 117 | +import { Switch } from 'react-native'; |
| 118 | +import { |
| 119 | + GestureDetector, |
| 120 | + GestureHandlerRootView, |
| 121 | + useTapGesture, |
| 122 | + createNativeWrapper, |
| 123 | +} from 'react-native-gesture-handler'; |
| 124 | +
|
| 125 | +const RNGHSwitch = createNativeWrapper(Switch); |
| 126 | +
|
| 127 | +export default function App() { |
| 128 | + const [isEnabled, setIsEnabled] = useState(false); |
| 129 | +
|
| 130 | + const tap1 = useTapGesture({ |
| 131 | + onDeactivate: () => { |
| 132 | + console.log('Tapped!'); |
| 133 | + }, |
| 134 | + }); |
| 135 | +
|
| 136 | + const tap2 = useTapGesture({ |
| 137 | + onDeactivate: () => { |
| 138 | + console.log('Tapped!'); |
| 139 | + }, |
| 140 | + }); |
| 141 | +
|
| 142 | + return ( |
| 143 | + <GestureHandlerRootView style={{ flex: 1, paddingTop: 100 }}> |
| 144 | + <GestureDetector gesture={tap1}> |
| 145 | + <Switch value={isEnabled} onValueChange={setIsEnabled} /> |
| 146 | + </GestureDetector> |
| 147 | + <GestureDetector gesture={tap2}> |
| 148 | + <RNGHSwitch value={isEnabled} onValueChange={setIsEnabled} /> |
| 149 | + </GestureDetector> |
| 150 | + </GestureHandlerRootView> |
| 151 | + ); |
| 152 | +} |
| 153 | +``` |
| 154 | +In this scenario, the `Switch` from React Native cannot be toggled on because the `tap1` gesture intercepts it. However, when wrapped with `createNativeWrapper`, the `RNGHSwitch` becomes capable of participating in the gesture recognition process. This setup allows the switch to be toggled on while still enabling `tap2` to recognize taps on it. |
0 commit comments