Skip to content

Commit 4790449

Browse files
Update hook usage in the NativeDetector (#3714)
## Description 1. Merges two `useEffects` with the same dependency array into one in `useGesture` 2. Adds `useEffect` responsible for updating the `propsRef` in `HostGestureDetector.web.tsx` 3. Removes `useEffects` which were only calling `detachHandlers` and replaces them with a cleanup function in `HostGestureDetector.web.tsx` --------- Co-authored-by: Andrzej Antoni Kwaśniewski <[email protected]>
1 parent d76522f commit 4790449

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
6262
};
6363

6464
useEffect(() => {
65-
detachHandlers(attachedNativeHandlerTags.current);
66-
}, [children]);
65+
propsRef.current = props;
66+
}, [props]);
6767

6868
useEffect(() => {
6969
if (React.Children.count(children) !== 1) {
@@ -72,14 +72,13 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
7272
);
7373
}
7474

75-
attachHandlers(new Set(handlerTags));
76-
}, [handlerTags, children]);
75+
const newHandlerTags = new Set(handlerTags);
76+
attachHandlers(newHandlerTags);
7777

78-
useEffect(() => {
7978
return () => {
80-
detachHandlers(attachedHandlerTags.current);
79+
detachHandlers(newHandlerTags);
8180
};
82-
}, []);
81+
}, [handlerTags, children]);
8382

8483
return (
8584
<View style={{ display: 'contents' }} ref={viewRef as Ref<View>}>

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,17 @@ export function useGesture<THandlerData, TConfig>(
9494
}, [type, tag]);
9595

9696
useEffect(() => {
97+
const preparedConfig = prepareConfig(config);
98+
RNGestureHandlerModule.setGestureHandlerConfig(tag, preparedConfig);
99+
RNGestureHandlerModule.flushOperations();
100+
97101
bindSharedValues(config, tag);
98102

99103
return () => {
100104
unbindSharedValues(config, tag);
101105
};
102106
}, [tag, config]);
103107

104-
useEffect(() => {
105-
const preparedConfig = prepareConfig(config);
106-
107-
RNGestureHandlerModule.setGestureHandlerConfig(tag, preparedConfig);
108-
109-
RNGestureHandlerModule.flushOperations();
110-
}, [config, tag]);
111-
112108
return {
113109
tag,
114110
type,

0 commit comments

Comments
 (0)