Skip to content

Commit af6d049

Browse files
authored
[General] Remove createNativeWrapper from Touchable (#4138)
## Description Replaces button wrapped with `createNativeWrapper` with a direct call to `useNativeGesture` and `NativeDetector`. ## Test plan According to the `Touchable stress test`, the version using `GestureDetector` and `useNativeGesture` directly is about 10% faster to render.
1 parent f1de825 commit af6d049

1 file changed

Lines changed: 28 additions & 26 deletions

File tree

  • packages/react-native-gesture-handler/src/v3/components/Touchable

packages/react-native-gesture-handler/src/v3/components/Touchable/Touchable.tsx

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
import React, { useCallback, useRef } from 'react';
22
import { Platform } from 'react-native';
33

4-
import type { ButtonProps } from '../../../components/GestureHandlerButton';
54
import GestureHandlerButton from '../../../components/GestureHandlerButton';
6-
import createNativeWrapper from '../../createNativeWrapper';
5+
import { NativeDetector } from '../../detectors/NativeDetector';
6+
import { useNativeGesture } from '../../hooks';
77
import type {
88
CallbackEventType,
99
EndCallbackEventType,
1010
TouchableProps,
1111
} from './TouchableProps';
1212

13-
const TouchableButton = createNativeWrapper<
14-
React.ComponentRef<typeof GestureHandlerButton>,
15-
ButtonProps
16-
>(GestureHandlerButton, {
17-
shouldCancelWhenOutside: true,
18-
shouldActivateOnStart: false,
19-
disallowInterruption: true,
20-
});
21-
2213
const isAndroid = Platform.OS === 'android';
2314
const TRANSPARENT_RIPPLE = { rippleColor: 'transparent' as const };
2415

@@ -135,6 +126,20 @@ export const Touchable = (props: TouchableProps) => {
135126
[onPressIn, onPressOut]
136127
);
137128

129+
const nativeGesture = useNativeGesture({
130+
onBegin,
131+
onActivate,
132+
onFinalize,
133+
onUpdate,
134+
hitSlop: props.hitSlop,
135+
testID: props.testID,
136+
enabled: !disabled,
137+
shouldCancelWhenOutside: cancelOnLeave,
138+
disableReanimated: true,
139+
shouldActivateOnStart: false,
140+
disallowInterruption: true,
141+
});
142+
138143
const rippleProps = shouldUseNativeRipple
139144
? {
140145
rippleColor: androidRipple?.color,
@@ -145,20 +150,17 @@ export const Touchable = (props: TouchableProps) => {
145150
: TRANSPARENT_RIPPLE;
146151

147152
return (
148-
<TouchableButton
149-
{...rest}
150-
{...rippleProps}
151-
ref={ref ?? null}
152-
enabled={!disabled}
153-
onBegin={onBegin}
154-
onActivate={onActivate}
155-
onFinalize={onFinalize}
156-
onUpdate={onUpdate}
157-
defaultOpacity={defaultOpacity}
158-
defaultUnderlayOpacity={defaultUnderlayOpacity}
159-
underlayColor={underlayColor}
160-
shouldCancelWhenOutside={cancelOnLeave}>
161-
{children}
162-
</TouchableButton>
153+
<NativeDetector gesture={nativeGesture}>
154+
<GestureHandlerButton
155+
{...rest}
156+
{...rippleProps}
157+
ref={ref ?? null}
158+
enabled={!disabled}
159+
defaultOpacity={defaultOpacity}
160+
defaultUnderlayOpacity={defaultUnderlayOpacity}
161+
underlayColor={underlayColor}>
162+
{children}
163+
</GestureHandlerButton>
164+
</NativeDetector>
163165
);
164166
};

0 commit comments

Comments
 (0)