Skip to content

Commit 580494d

Browse files
committed
use callback in native detector
1 parent cd793b3 commit 580494d

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

packages/react-native-gesture-handler/src/v3/HostGestureDetector.web.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef } from 'react';
1+
import { useEffect, useRef, useCallback } from 'react';
22
import { View } from 'react-native';
33
import RNGestureHandlerModule from '../RNGestureHandlerModule.web';
44
import { ActionType } from '../ActionType';
@@ -15,8 +15,20 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
1515
const viewRef = useRef(null);
1616
const propsRef = useRef<GestureHandlerDetectorProps>(props);
1717
const oldHandlerTags = useRef<Set<number>>(new Set<number>());
18+
const oldDispatchesAnimatedEvents = useRef<boolean>(null);
1819

19-
const attachHandlers = () => {
20+
const detachHandlers = useCallback(() => {
21+
handlerTags.forEach((tag) => {
22+
RNGestureHandlerModule.detachGestureHandler(tag);
23+
});
24+
}, [handlerTags]);
25+
26+
const attachHandlers = useCallback(() => {
27+
if (oldDispatchesAnimatedEvents.current !== dispatchesAnimatedEvents) {
28+
// We reattach handlers, if the action type changes
29+
oldHandlerTags.current = new Set<number>();
30+
detachHandlers();
31+
}
2032
const currentHandlerTags = new Set(handlerTags);
2133
const newHandlerTags = currentHandlerTags.difference(
2234
oldHandlerTags.current
@@ -32,13 +44,7 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
3244
);
3345
});
3446
oldHandlerTags.current = currentHandlerTags;
35-
};
36-
37-
const detachHandlers = () => {
38-
handlerTags.forEach((tag) => {
39-
RNGestureHandlerModule.detachGestureHandler(tag);
40-
});
41-
};
47+
}, [handlerTags, dispatchesAnimatedEvents, detachHandlers]);
4248

4349
useEffect(() => {
4450
attachHandlers();

0 commit comments

Comments
 (0)