|
| 1 | +--- |
| 2 | +id: reanimated-interactions |
| 3 | +title: Integration with Reanimated |
| 4 | +sidebar_label: Integration with Reanimated |
| 5 | +sidebar_position: 4 |
| 6 | +--- |
| 7 | + |
| 8 | +import CollapsibleCode from '@site/src/components/CollapsibleCode'; |
| 9 | + |
| 10 | +`GestureDetector` will decide whether to use [Reanimated](https://docs.swmansion.com/react-native-reanimated/) to process provided gestures based on their configuration. If any of the callbacks is a [worklet](<(https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary/#worklet)>) and Reanimated is not explicitly turned off, tools provided by the Reanimated will be utilized bringing ability to handle gestures synchronously. |
| 11 | + |
| 12 | +## Automatic [workletization](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary/#to-workletize) of gesture callbacks |
| 13 | + |
| 14 | +[Worklets' Babel plugin](https://docs.swmansion.com/react-native-worklets/docs/worklets-babel-plugin/about) is setup in a way that automatically marks callbacks passed to gestures in the configuration chain as worklets. This means that you don't need to add a `'worklet';` directive at the beginning of the functions. Here is an example that will be automatically workletized: |
| 15 | + |
| 16 | +```jsx |
| 17 | +const gesture = useTapGesture({ |
| 18 | + onBegin: () => { |
| 19 | + console.log(_WORKLET); |
| 20 | + }, |
| 21 | +}); |
| 22 | +``` |
| 23 | + |
| 24 | +And here is one that won't: |
| 25 | + |
| 26 | +```jsx |
| 27 | +const callback = () => { |
| 28 | + console.log(_WORKLET); |
| 29 | +}; |
| 30 | + |
| 31 | +const gesture = useTapGesture({ |
| 32 | + onBegin: callback, |
| 33 | +}); |
| 34 | +``` |
| 35 | + |
| 36 | +It also won't work when wrapped with hooks like `useCallback` or `useMemo`, e.g: |
| 37 | + |
| 38 | +```jsx |
| 39 | +const callback = useCallback(() => { |
| 40 | + console.log(_WORKLET); |
| 41 | +}, []); |
| 42 | + |
| 43 | +const gesture = useTapGesture({ |
| 44 | + onBegin: callback, |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +In the above cases, you should add a `"worklet";` directive at the beginning of the callbacks, like so: |
| 49 | + |
| 50 | +```jsx |
| 51 | +const callback = () => { |
| 52 | + // highlight-next-line |
| 53 | + 'worklet'; |
| 54 | + console.log(_WORKLET); |
| 55 | +}; |
| 56 | + |
| 57 | +const gesture = useTapGesture({ |
| 58 | + onBegin: callback, |
| 59 | +}); |
| 60 | +``` |
| 61 | + |
| 62 | +```jsx |
| 63 | +const callback = useCallback(() => { |
| 64 | + // highlight-next-line |
| 65 | + 'worklet;'; |
| 66 | + console.log(_WORKLET); |
| 67 | +}, []); |
| 68 | + |
| 69 | +const gesture = useTapGesture({ |
| 70 | + onBegin: callback, |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +## Using SharedValue in gesture config |
| 75 | + |
| 76 | +RNGH3 allows to pass [`SharedValue`](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#shared-value) to gestures' configurations. This allows to react to configuration changes without unnecessary rerenders. |
| 77 | + |
| 78 | +<CollapsibleCode |
| 79 | +label="Show full example" |
| 80 | +expandedLabel="Hide full example" |
| 81 | +lineBounds={[10, 19]} |
| 82 | +src={` |
| 83 | +import * as React from 'react'; |
| 84 | +import { Animated } from 'react-native'; |
| 85 | +import { |
| 86 | + GestureHandlerRootView, |
| 87 | + GestureDetector, |
| 88 | + useTapGesture, |
| 89 | +} from 'react-native-gesture-handler'; |
| 90 | +import { useSharedValue } from 'react-native-reanimated'; |
| 91 | +
|
| 92 | +export default function App() { |
| 93 | + // highlight-next-line |
| 94 | + const taps = useSharedValue(2); |
| 95 | +
|
| 96 | + const gesture = useTapGesture({ |
| 97 | + // highlight-next-line |
| 98 | + numberOfTaps: taps, |
| 99 | + onDeactivate: () => { |
| 100 | + taps.value += 1; |
| 101 | + }, |
| 102 | + }); |
| 103 | +
|
| 104 | + return ( |
| 105 | + <GestureHandlerRootView> |
| 106 | + <GestureDetector gesture={gesture}> |
| 107 | + <Animated.View |
| 108 | + style={{ |
| 109 | + width: 150, |
| 110 | + height: 150, |
| 111 | + backgroundColor: 'blue', |
| 112 | + }} |
| 113 | + /> |
| 114 | + </GestureDetector> |
| 115 | + </GestureHandlerRootView> |
| 116 | + ); |
| 117 | +} |
| 118 | +`}/> |
0 commit comments