Skip to content

Commit 35cb78c

Browse files
authored
[General] Only overwrite ref if the dropped handler didn't change (#3984)
## Description It's possible that when a gesture is updated, the new values in the ref will be overwritten after being updated, due to the cleanup not checking what it's deleting. It was possible to get this scenario: ``` LOG useGesture createGestureHandler TapGestureHandler 1 previous -1 LOG useGesture useEffect TapGestureHandler 1 LOG useGesture createGestureHandler TapGestureHandler 2 previous TapGestureHandler 1 LOG useGesture useEffect cleanup TapGestureHandler 1 LOG useGesture createGestureHandler TapGestureHandler 2 previous -1 ``` where the last update would crash due to the ref being cleared by the cleanup of `TapGestureHandler 1` AFTER being updated by `TapGestureHandler 2`. ## Test plan File 1: ```jsx import React from 'react'; import { View } from 'react-native'; import { GestureDetector, useTapGesture } from 'react-native-gesture-handler'; function Test() { const gesture = useTapGesture({ onActivate: () => { console.log('onActivate'); }, }); const COMMENT_THIS_FIRST = 2; return ( <GestureDetector gesture={gesture}> <View style={{ width: 100, height: 100, backgroundColor: 'red' }} /> </GestureDetector> ); } export default Test; ``` File2: ```jsx import React from 'react'; import { GestureHandlerRootView } from 'react-native-gesture-handler'; import Test from './File1'; function Example() { let COMMENT_THIS_SECOND = 1; return ( <GestureHandlerRootView> <Test /> </GestureHandlerRootView> ); } export default Example; ``` Comment out `COMMENT_THIS_FIRST`, save, then comment out `COMMENT_THIS_SECOND` and save.
1 parent 81c5b06 commit 35cb78c

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ export function useGesture<THandlerData, TConfig>(
9090

9191
useEffect(() => {
9292
return () => {
93-
currentGestureRef.current = { type: '', handlerTag: -1 };
93+
if (currentGestureRef.current.handlerTag === handlerTag) {
94+
currentGestureRef.current = { type: '', handlerTag: -1 };
95+
}
96+
9497
NativeProxy.dropGestureHandler(handlerTag);
9598
scheduleFlushOperations();
9699
};

0 commit comments

Comments
 (0)