|
| 1 | +--- |
| 2 | +id: state-manager |
| 3 | +title: State manager |
| 4 | +sidebar_label: State manager |
| 5 | +sidebar_position: 6 |
| 6 | +--- |
| 7 | + |
| 8 | +import CollapsibleCode from '@site/src/components/CollapsibleCode'; |
| 9 | + |
| 10 | +RNGH3 allows to manually control gestures lifecycle by using `GestureStateManager`. |
| 11 | + |
| 12 | +## State management |
| 13 | + |
| 14 | +Manual state management is based on `handlerTag`. There are two ways of manual state control. |
| 15 | + |
| 16 | +### Inside gesture definition |
| 17 | + |
| 18 | +If you want to manipulate gesture's state in its callbacks, you can get `handlerTag` from event parameter. |
| 19 | + |
| 20 | +<CollapsibleCode |
| 21 | +label="Show full example" |
| 22 | +expandedLabel="Hide full example" |
| 23 | +lineBounds={[9, 17]} |
| 24 | +src={` |
| 25 | +import { StyleSheet, View } from 'react-native'; |
| 26 | +import { |
| 27 | + GestureDetector, |
| 28 | + GestureHandlerRootView, |
| 29 | + GestureStateManager, |
| 30 | + useLongPressGesture, |
| 31 | +} from 'react-native-gesture-handler'; |
| 32 | +
|
| 33 | +export default function App() { |
| 34 | + const longPress = useLongPressGesture({ |
| 35 | + onTouchesDown: (e) => { |
| 36 | + // highlight-next-line |
| 37 | + GestureStateManager.activate(e.handlerTag); |
| 38 | + }, |
| 39 | + onActivate: () => { |
| 40 | + console.log('LongPress activated!'); |
| 41 | + }, |
| 42 | + }); |
| 43 | +
|
| 44 | + return ( |
| 45 | + <GestureHandlerRootView style={styles.container}> |
| 46 | + <GestureDetector gesture={longPress}> |
| 47 | + <View style={styles.box} /> |
| 48 | + </GestureDetector> |
| 49 | + </GestureHandlerRootView> |
| 50 | + ); |
| 51 | +} |
| 52 | +
|
| 53 | +const styles = StyleSheet.create({ |
| 54 | + container: { |
| 55 | + flex: 1, |
| 56 | + alignItems: 'center', |
| 57 | + justifyContent: 'space-around', |
| 58 | + }, |
| 59 | +
|
| 60 | + box: { |
| 61 | + width: 150, |
| 62 | + height: 150, |
| 63 | + backgroundColor: 'blue', |
| 64 | + }, |
| 65 | +}); |
| 66 | +`}/> |
| 67 | + |
| 68 | + |
| 69 | +### Outside gesture definition |
| 70 | + |
| 71 | +If you want to control gesture lifecycle outside of it, you can use `handlerTag` from created gesture object. |
| 72 | + |
| 73 | +<CollapsibleCode |
| 74 | +label="Show full example" |
| 75 | +expandedLabel="Hide full example" |
| 76 | +lineBounds={[10, 21]} |
| 77 | +src={` |
| 78 | +import { StyleSheet, View } from 'react-native'; |
| 79 | +import { |
| 80 | + GestureDetector, |
| 81 | + GestureHandlerRootView, |
| 82 | + GestureStateManager, |
| 83 | + useLongPressGesture, |
| 84 | + usePanGesture, |
| 85 | +} from 'react-native-gesture-handler'; |
| 86 | +
|
| 87 | +export default function App() { |
| 88 | + const pan = usePanGesture({ |
| 89 | + onActivate: () => { |
| 90 | + console.log('Pan activated!'); |
| 91 | + }, |
| 92 | + }); |
| 93 | +
|
| 94 | + const longPress = useLongPressGesture({ |
| 95 | + onActivate: () => { |
| 96 | + // highlight-next-line |
| 97 | + GestureStateManager.activate(pan.handlerTag); |
| 98 | + }, |
| 99 | + }); |
| 100 | +
|
| 101 | + return ( |
| 102 | + <GestureHandlerRootView style={styles.container}> |
| 103 | + <GestureDetector gesture={longPress}> |
| 104 | + <View style={styles.box} /> |
| 105 | + </GestureDetector> |
| 106 | + <GestureDetector gesture={pan}> |
| 107 | + <View style={styles.box} /> |
| 108 | + </GestureDetector> |
| 109 | + </GestureHandlerRootView> |
| 110 | + ); |
| 111 | +} |
| 112 | +
|
| 113 | +const styles = StyleSheet.create({ |
| 114 | + container: { |
| 115 | + flex: 1, |
| 116 | + alignItems: 'center', |
| 117 | + justifyContent: 'space-around', |
| 118 | + }, |
| 119 | +
|
| 120 | + box: { |
| 121 | + width: 150, |
| 122 | + height: 150, |
| 123 | + backgroundColor: 'blue', |
| 124 | + }, |
| 125 | +}); |
| 126 | +`}/> |
| 127 | + |
| 128 | +## Methods |
| 129 | + |
| 130 | +### begin |
| 131 | + |
| 132 | +```tsx |
| 133 | +begin: (handlerTag: number) => void; |
| 134 | +``` |
| 135 | + |
| 136 | +Triggers [`onBegin`](/docs/fundamentals/callbacks-events#onbegin) callback on gesture with specified `handlerTag`. |
| 137 | + |
| 138 | +### activate |
| 139 | + |
| 140 | +```tsx |
| 141 | +activate: (handlerTag: number) => void; |
| 142 | +``` |
| 143 | + |
| 144 | +Triggers [`onActivate`](/docs/fundamentals/callbacks-events#onactivate) callback on gesture with specified `handlerTag`. |
| 145 | + |
| 146 | +### deactivate |
| 147 | + |
| 148 | +```tsx |
| 149 | +deactivate: (handlerTag: number) => void; |
| 150 | +``` |
| 151 | + |
| 152 | +Triggers first [`onDeactivate`](/docs/fundamentals/callbacks-events#ondeactivate), then [`onFinalize`](/docs/fundamentals/callbacks-events#onfinalize) callbacks on gesture with specified `handlerTag`. |
| 153 | + |
| 154 | +### fail |
| 155 | + |
| 156 | +```tsx |
| 157 | +fail: (handlerTag: number) => void; |
| 158 | +``` |
| 159 | + |
| 160 | +Triggers [`onFinalize`](/docs/fundamentals/callbacks-events#onfinalize) callback on gesture with specified `handlerTag`. If gesture had activated, it will also trigger [`onDeactivate`](/docs/fundamentals/callbacks-events#ondeactivate) callback. |
0 commit comments