Skip to content

Commit 19ea1a9

Browse files
committed
Memoize intercepting detector props
1 parent a8549b0 commit 19ea1a9

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

packages/react-native-gesture-handler/src/v3/detectors/VirtualDetector/InterceptingGestureDetector.tsx

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,23 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({
8181
);
8282
}
8383

84-
const handleGestureEvent = (key: keyof DetectorCallbacks<THandlerData>) => {
85-
return (e: GestureHandlerEvent<THandlerData>) => {
86-
if (gesture?.detectorCallbacks[key]) {
87-
gesture.detectorCallbacks[key](e);
88-
}
89-
90-
virtualChildren.forEach((child) => {
91-
const method = child.methods[key];
92-
if (method) {
93-
method(e);
84+
const createGestureEventHandler = useCallback(
85+
(key: keyof DetectorCallbacks<THandlerData>) => {
86+
return (e: GestureHandlerEvent<THandlerData>) => {
87+
if (gesture?.detectorCallbacks[key]) {
88+
gesture.detectorCallbacks[key](e);
9489
}
95-
});
96-
};
97-
};
90+
91+
virtualChildren.forEach((child) => {
92+
const method = child.methods[key];
93+
if (method) {
94+
method(e);
95+
}
96+
});
97+
};
98+
},
99+
[gesture, virtualChildren]
100+
);
98101

99102
const getHandlers = useCallback(
100103
(key: keyof DetectorCallbacks<unknown>) => {
@@ -152,22 +155,34 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({
152155
configureRelations(gesture);
153156
}
154157

158+
const handlerTags = useMemo(() => {
159+
if (gesture) {
160+
return isComposedGesture(gesture) ? gesture.tags : [gesture.tag];
161+
}
162+
return [];
163+
}, [gesture]);
164+
155165
return (
156166
<DetectorContext value={contextValue}>
157167
<NativeDetectorComponent
158168
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
159-
onGestureHandlerStateChange={handleGestureEvent(
160-
'onGestureHandlerStateChange'
169+
onGestureHandlerStateChange={useMemo(
170+
() => createGestureEventHandler('onGestureHandlerStateChange'),
171+
[createGestureEventHandler]
161172
)}
162173
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
163-
onGestureHandlerEvent={handleGestureEvent('onGestureHandlerEvent')}
174+
onGestureHandlerEvent={useMemo(
175+
() => createGestureEventHandler('onGestureHandlerEvent'),
176+
[createGestureEventHandler]
177+
)}
164178
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
165179
onGestureHandlerAnimatedEvent={
166180
gesture?.detectorCallbacks.onGestureHandlerAnimatedEvent
167181
}
168182
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
169-
onGestureHandlerTouchEvent={handleGestureEvent(
170-
'onGestureHandlerTouchEvent'
183+
onGestureHandlerTouchEvent={useMemo(
184+
() => createGestureEventHandler('onGestureHandlerTouchEvent'),
185+
[createGestureEventHandler]
171186
)}
172187
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
173188
onGestureHandlerReanimatedStateChange={
@@ -181,13 +196,7 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({
181196
onGestureHandlerReanimatedTouchEvent={
182197
shouldUseReanimated ? reanimatedTouchEventHandler : undefined
183198
}
184-
handlerTags={
185-
gesture
186-
? isComposedGesture(gesture)
187-
? gesture.tags
188-
: [gesture.tag]
189-
: []
190-
}
199+
handlerTags={handlerTags}
191200
style={nativeDetectorStyles.detector}
192201
virtualChildren={virtualChildren}
193202
moduleId={globalThis._RNGH_MODULE_ID}>

0 commit comments

Comments
 (0)