-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[docs] Add "Wrapped components" section #4119
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
161f0ce
Explain components
m-bert 55f7e0e
Merge branch 'main' into @mbert/docs-components
m-bert 71bbf47
Review
m-bert e1f1b36
Review v2
m-bert 2e99678
Add example
m-bert 942a8fa
Merge branch 'main' into @mbert/docs-components
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
90 changes: 90 additions & 0 deletions
90
packages/docs-gesture-handler/docs/components/wrapped-components.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,90 @@ | ||
| --- | ||
| id: wrapped-components | ||
| title: Wrapped Components | ||
| sidebar_label: Wrapped Components | ||
| --- | ||
|
|
||
| Some components come with a [`Native`](/docs/gestures/use-native-gesture) gesture pre-applied. This allows them to participate in the gesture recognition process. Have a look at the example below. | ||
|
|
||
| ```tsx | ||
| import { useState } from 'react'; | ||
| import { Switch } from 'react-native'; | ||
| import { | ||
| GestureDetector, | ||
| GestureHandlerRootView, | ||
| useTapGesture, | ||
| Switch as RNGHSwitch, | ||
| } from 'react-native-gesture-handler'; | ||
|
|
||
| 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> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| On Android, in this scenario, the `Switch` from React Native cannot be toggled on because the `tap1` gesture intercepts it. However, using `RNGHSwitch` makes it 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. | ||
|
|
||
| ## List of wrapped components | ||
|
|
||
| Components listed below come with a pre-applied `Native` gesture. | ||
|
|
||
| - `FlatList` | ||
| - `ScrollView` | ||
| - `RefreshControl` | ||
| - `TextInput` | ||
| - `Switch` | ||
|
|
||
| ## onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER | ||
|
|
||
| :::danger | ||
| This callback may lead to infinite re-renders if not used carefully. | ||
|
j-piasecki marked this conversation as resolved.
|
||
|
|
||
| ```tsx | ||
| export default function App() { | ||
| const [gesture, setGesture] = useState<NativeGesture | null>(null); | ||
|
|
||
| const updateGesture = (g: NativeGesture) => { | ||
| // ❌ Wrong usage: calling setState here triggers a re-render, | ||
| // which re-creates the ScrollView's Native gesture, which fires | ||
| // this callback again → infinite re-render loop. | ||
| setGesture(g); | ||
| }; | ||
|
|
||
| return ( | ||
| <GestureHandlerRootView style={{ flex: 1 }}> | ||
| <ScrollView onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER={updateGesture} /> | ||
| </GestureHandlerRootView> | ||
| ); | ||
| } | ||
| ``` | ||
| ::: | ||
|
|
||
| Those components also receive an additional prop named `onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER`. | ||
|
|
||
| ```ts | ||
| onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER?: (gesture: NativeGesture) => void; | ||
| ``` | ||
|
|
||
| This callback is invoked when the wrapped component's underlying `Native` gesture instance or configuration changes, providing access to the underlying gesture. This can be helpful when setting up [relations](/docs/fundamentals/gesture-composition) with other gestures. You can check example usage in our [`ScrollView`](https://github.com/software-mansion/react-native-gesture-handler/blob/main/packages/react-native-gesture-handler/src/v3/components/GestureComponents.tsx#L78) component. | ||
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.
Uh oh!
There was an error while loading. Please reload this page.