Skip to content

Commit f87ac60

Browse files
authored
Fix gesture detach on component drop (#3669)
## Description Gestures were not detached properly on Android on component drop. ## Test plan ### Android component drop Add a logging message in attach/detach in `android/.../react/RNGestureHandlerRegistry.kt` ```ts import * as React from 'react'; import { Animated, Button } from 'react-native'; import { GestureHandlerRootView, NativeDetector, useGesture, } from 'react-native-gesture-handler'; export default function App() { const [visible, setVisible] = React.useState(true); const gesture = useGesture('PanGestureHandler', { onBegin: (e: any) => { console.log('onGestureHandlerEvent', e.nativeEvent.handlerData); }, onUpdate: (e: any) => { const { translationX, translationY } = e.nativeEvent.handlerData; const distance = Math.sqrt(translationX ** 2 + translationY ** 2); if (distance > 100) { setVisible(false); } }, }); return ( <GestureHandlerRootView style={{ flex: 1, backgroundColor: 'white', paddingTop: 100 }}> <Button title="Toggle visibility" onPress={() => { setVisible(!visible); }} /> {visible && ( <NativeDetector gesture={gesture}> <Animated.View style={{ width: 150, height: 150, backgroundColor: 'blue', opacity: 0.5, borderWidth: 10, borderColor: 'red', marginTop: 20, marginLeft: 40, }} /> </NativeDetector> )} </GestureHandlerRootView> ); } ```
1 parent 8b0f128 commit f87ac60

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerDetectorView.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
124124
eventDispatcher?.dispatchEvent(event)
125125
}
126126

127+
fun onViewDrop() {
128+
val registry = RNGestureHandlerModule.registries[moduleId]
129+
?: throw Exception("Tried to access a non-existent registry")
130+
131+
for (tag in attachedHandlers) {
132+
registry.detachHandler(tag)
133+
attachedHandlers.remove(tag)
134+
}
135+
}
136+
127137
companion object {
128138
private enum class GestureHandlerMutation {
129139
Attach,

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerDetectorViewManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ class RNGestureHandlerDetectorViewManager :
3636
override fun setModuleId(view: RNGestureHandlerDetectorView, value: Int) {
3737
view.setModuleId(value)
3838
}
39+
40+
override fun onDropViewInstance(view: RNGestureHandlerDetectorView) {
41+
view.onViewDrop()
42+
super.onDropViewInstance(view)
43+
}
3944
}

0 commit comments

Comments
 (0)