Skip to content

Commit 3824142

Browse files
committed
Docs
1 parent b0f4868 commit 3824142

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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`](/docs/gestures/use-native-gesture) 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: NativeWrapperProperties,
26+
detectorType: GestureDetectorType,
27+
): React.FC<P>
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`](/docs/gestures/use-native-gesture) 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+
## Components wrapped with createNativeWrapper
61+
62+
Some of Gesture Handler's components are already wrapped with `createNativeWrapper` by default. These include:
63+
64+
- `Switch`
65+
- `TextInput`
66+
- `ScrollView`
67+
- `FlatList`
68+
- `RefreshControl`
69+
- `RawButton`
70+
- `BaseButton`
71+
- `RectButton`
72+
- `BorderlessButton`
73+
74+
## Example
75+
76+
This example only demonstrates the usage of `createNativeWrapper`. `Switch` component from Gesture Handler comes wrapped with `createNativeWrapper` by default.
77+
78+
```tsx
79+
import { useState } from 'react';
80+
import { Switch } from 'react-native';
81+
import {
82+
GestureDetector,
83+
GestureHandlerRootView,
84+
useTapGesture,
85+
createNativeWrapper,
86+
} from 'react-native-gesture-handler';
87+
88+
const RNGHSwitch = createNativeWrapper(Switch);
89+
90+
export default function App() {
91+
const [isEnabled, setIsEnabled] = useState(false);
92+
93+
const tap1 = useTapGesture({
94+
onDeactivate: () => {
95+
console.log('Tapped!');
96+
},
97+
});
98+
99+
const tap2 = useTapGesture({
100+
onDeactivate: () => {
101+
console.log('Tapped!');
102+
},
103+
});
104+
105+
return (
106+
<GestureHandlerRootView style={{ flex: 1, paddingTop: 100 }}>
107+
<GestureDetector gesture={tap1}>
108+
<Switch value={isEnabled} onValueChange={setIsEnabled} />
109+
</GestureDetector>
110+
<GestureDetector gesture={tap2}>
111+
<RNGHSwitch value={isEnabled} onValueChange={setIsEnabled} />
112+
</GestureDetector>
113+
</GestureHandlerRootView>
114+
);
115+
}
116+
```
117+
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

Comments
 (0)