Commit 4a1aba5
fix: gesture not activating due to outdated handler tag (#3578)
## Description
<!--
Description and motivation for this PR.
Include 'Fixes #<number>' if this is fixing some issue.
-->
This fixes an issue where the manual activation of a handler wouldn't
work.
I unfortunately didn't had time to make a full reproduction. The
scenario roughly looks like this:
```ts
const gesture = useMemo(() -> Gesture.Pan() // Note happens also without useMemo
.manualActivation(true)
.onTouchesMove((event, manager) => {
// Some criteria to met:
manager.activate()
})
.onUpdate(() => {
// Wouldn't get called on a remount
})
return <GestureDetector gesture={gesture}> // ...
```
This is being used in a component on a screen. When we navigate from the
screen the component gets unmounted and I can see that his logic is
being triggered:
https://github.com/software-mansion/react-native-gesture-handler/blob/1e1f4e035ea6d4c38684c4abc43d91dbf5b46dc6/packages/react-native-gesture-handler/src/handlers/gestures/GestureDetector/index.tsx#L152-L168
```
attachHandler 1
dropHandler 1
```
I think the screen might get frozen instead of unmounted, but not
entirely sure, but the cleanup function gets called and the handler gets
dropped.
Now, when reopening the screen the handlers will reattach, but have a
different handler tag now.
```
attachHandler 1
dropHandler 1
attachHandler 2
```
When I now run the gesture I can see that the `manager` still has the
**previous handler tag**, and thus on the native side it can't find the
handler to activate. Its calling `setGestureHandlerState(1 /* previous
id */)` here (when I call `manager.activate`):
https://github.com/software-mansion/react-native-gesture-handler/blob/1e1f4e035ea6d4c38684c4abc43d91dbf5b46dc6/packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerModule.kt#L108
I found that attaching the handler tag to the GestureStateMangaer and
recreating it if it has changed fixes the issue.
If you need a full reproduction to proceed with this change I can
understand, just let me know then I can try to find time to create one 👍
## Test plan
<!--
Describe how did you test this change here.
-->
Well, shamefully I didn't create a reproduction so I guess the test plan
is "trust me bro" 😅 not the best I know - sorry.
---------
Co-authored-by: Michał Bert <63123542+m-bert@users.noreply.github.com>
Co-authored-by: Michał <michal.bert@swmansion.com>1 parent 7230eda commit 4a1aba5
3 files changed
Lines changed: 10 additions & 1 deletion
File tree
- packages/react-native-gesture-handler/src/handlers/gestures
- GestureDetector
Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
166 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
167 | 170 | | |
168 | 171 | | |
169 | 172 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| 34 | + | |
| 35 | + | |
32 | 36 | | |
33 | 37 | | |
34 | 38 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
0 commit comments