Commit 35cb78c
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
Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
94 | 97 | | |
95 | 98 | | |
96 | 99 | | |
| |||
0 commit comments