-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Export new createNativeWrapper
#3971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+180
−4
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
105bf9a
migration guide
m-bert b0f4868
Update skill
m-bert 3824142
Docs
m-bert c992498
Mention dedicated section
m-bert 4f54383
Update return type
m-bert 37b4e2f
Update skill
m-bert 52ce54e
Merge branch 'main' into @mbert/export-create-native-wrapper
m-bert 319914c
Mention onGestureUpdate
m-bert 81b4c3a
Merge branch 'main' into @mbert/export-create-native-wrapper
m-bert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
packages/docs-gesture-handler/docs/components/create-native-wrapper.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| --- | ||
| id: create-native-wrapper | ||
| title: createNativeWrapper | ||
| sidebar_label: createNativeWrapper | ||
| --- | ||
|
|
||
| `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. | ||
|
|
||
| ```tsx | ||
| import { Switch } from 'react-native'; | ||
| import { createNativeWrapper } from 'react-native-gesture-handler'; | ||
|
|
||
| const RNGHSwitch = createNativeWrapper(Switch); | ||
| ``` | ||
|
|
||
| Full example can be seen in the [Example section](#example) below. | ||
|
|
||
| This function can be useful when you want some third-party components to participate in gesture recognition process. | ||
|
|
||
| ## createNativeWrapper | ||
|
|
||
| ```ts | ||
| function createNativeWrapper<P>( | ||
| Component: React.ComponentType<P>, | ||
| config: Readonly<NativeWrapperProperties> = {}, | ||
| detectorType: GestureDetectorType = GestureDetectorType.Native, | ||
| ): React.FC<P & NativeWrapperProperties> | ||
| ``` | ||
|
|
||
| [`config`](#config) and [`detectorType`](#detectortype) parameters are optional. Their default values are described in their respective sections below. | ||
|
|
||
| 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: | ||
|
|
||
| ```tsx | ||
| <DetectorComponent gesture={native}> | ||
| <Component {...childProps} /> | ||
| </DetectorComponent> | ||
| ``` | ||
|
|
||
| ### Component | ||
|
|
||
| Component to be wrapped with `Native gesture`. It can be any React component, including those from third-party libraries. | ||
|
|
||
| ### config | ||
|
|
||
| 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. | ||
|
|
||
| ### detectorType | ||
|
|
||
| ```ts | ||
| enum GestureDetectorType { | ||
| Native, | ||
| Virtual, | ||
| Intercepting, | ||
| } | ||
| ``` | ||
|
|
||
| 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)). | ||
|
|
||
| ## onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER | ||
|
|
||
| :::danger | ||
| This callback may lead to infinite re-renders if not used carefully. | ||
| ::: | ||
|
|
||
| ```ts | ||
| onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER?: (gesture: NativeGesture) => void; | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
|
|
||
| To prevent infinite re-renders, ensure you are not updating the component with the same gesture repeatedly, e.g.: | ||
|
|
||
| ```tsx | ||
| const WrappedComponent = createNativeWrapper(Component); | ||
|
|
||
| const ParentComponent = () => { | ||
| const [nativeGesture, setNativeGesture] = useState<NativeGesture | null>( | ||
| null | ||
| ); | ||
|
|
||
| const onGestureUpdate = (gesture: NativeGesture) => { | ||
| if (!nativeGesture || nativeGesture.handlerTag !== gesture.handlerTag) { | ||
| setNativeGesture(gesture); | ||
| ... | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| <WrappedComponent | ||
| ... | ||
| onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER={onGestureUpdate} | ||
| />; | ||
| ``` | ||
|
|
||
| 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). | ||
|
|
||
| ## Components wrapped using createNativeWrapper | ||
|
|
||
| Gesture Handler reexports some of the React Native components that are already wrapped using `createNativeWrapper`. These include: | ||
|
|
||
| - `Switch` | ||
| - `TextInput` | ||
| - `ScrollView` | ||
| - `FlatList` | ||
| - `RefreshControl` | ||
|
|
||
| [Buttons](/docs/components/buttons) are also wrapped using `createNativeWrapper` by default. | ||
|
|
||
| ## Example | ||
|
|
||
| This example only demonstrates the usage of `createNativeWrapper`. `Switch` component from Gesture Handler comes wrapped with `createNativeWrapper` by default. | ||
|
|
||
| ```tsx | ||
| import { useState } from 'react'; | ||
| import { Switch } from 'react-native'; | ||
| import { | ||
| GestureDetector, | ||
| GestureHandlerRootView, | ||
| useTapGesture, | ||
| createNativeWrapper, | ||
| } from 'react-native-gesture-handler'; | ||
|
|
||
| const RNGHSwitch = createNativeWrapper(Switch); | ||
|
|
||
| export default function App() { | ||
| const [isEnabled, setIsEnabled] = useState(false); | ||
|
|
||
| const tap1 = useTapGesture({ | ||
| onDeactivate: () => { | ||
| console.log('Tapped!'); | ||
| }, | ||
| }); | ||
|
|
||
| const tap2 = useTapGesture({ | ||
| onDeactivate: () => { | ||
| console.log('Tapped!'); | ||
| }, | ||
| }); | ||
|
|
||
| return ( | ||
| <GestureHandlerRootView style={{ flex: 1, paddingTop: 100 }}> | ||
| <GestureDetector gesture={tap1}> | ||
| <Switch value={isEnabled} onValueChange={setIsEnabled} /> | ||
| </GestureDetector> | ||
| <GestureDetector gesture={tap2}> | ||
| <RNGHSwitch value={isEnabled} onValueChange={setIsEnabled} /> | ||
| </GestureDetector> | ||
| </GestureHandlerRootView> | ||
| ); | ||
| } | ||
| ``` | ||
| 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. | ||
j-piasecki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs suggest you can switch to
GestureDetectorType.Virtualby just passingdetectorType, but the underlyingVirtualGestureDetectorrequires being rendered under anInterceptingGestureDetector(it throws otherwise). Consider adding an explicit note/example showing the requiredInterceptingGestureDetectorwrapper when usingGestureDetectorType.Virtual.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is described in the gesture detectors section, so I'm not sure if it is required here. If someone doesn't know how it works, they will probably have to look into these docs anyway (cc @akwasniewski)