Skip to content

Commit 7406046

Browse files
authored
Memoize Text component (#4003)
## Description This PR introduces 3 changes to `Text` component - Removes `forwardedRef` - Adds memoization ## Test plan Tested on "Nested Text" example
1 parent 19b619b commit 7406046

1 file changed

Lines changed: 41 additions & 47 deletions

File tree

  • packages/react-native-gesture-handler/src/components
Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import React, {
2-
ForwardedRef,
3-
forwardRef,
4-
RefObject,
5-
useEffect,
6-
useRef,
7-
} from 'react';
1+
import React, { ComponentRef, Ref, useEffect, useMemo, useRef } from 'react';
82
import {
93
Platform,
104
Text as RNText,
@@ -14,23 +8,25 @@ import {
148
import { GestureObjects as Gesture } from '../handlers/gestures/gestureObjects';
159
import { GestureDetector } from '../handlers/gestures/GestureDetector';
1610

11+
type TextProps = RNTextProps & {
12+
ref?: Ref<ComponentRef<typeof RNText> | null>;
13+
};
14+
type RNGHTextRef = Ref<RNText | null> & { rngh?: boolean };
15+
1716
/**
18-
* @deprecated `LegacyText` is deprecated. Since Gesture Handler 3 you can wrap `Text` with `GestureDetector`.
17+
* @deprecated `LegacyText` is deprecated. Since Gesture Handler 3, you should wrap `Text` with `GestureDetector`, `InterceptingGestureDetector`, or `VirtualGestureDetector`.
1918
*/
20-
export const LegacyText = forwardRef(
21-
(
22-
props: RNTextProps,
23-
ref: ForwardedRef<React.ComponentRef<typeof RNText>>
24-
) => {
25-
const { onPress, onLongPress, ...rest } = props;
19+
export const LegacyText = (props: TextProps) => {
20+
const { onPress, onLongPress, ref, ...rest } = props;
2621

27-
const textRef = useRef<RNText | null>(null);
28-
const native = Gesture.Native().runOnJS(true);
22+
const textRef = useRef<RNText | null>(null);
23+
const native = useMemo(() => Gesture.Native().runOnJS(true), []);
2924

30-
const refHandler = (node: any) => {
25+
const refHandler = useMemo(() => {
26+
const handler: RNGHTextRef = (node: RNText | null) => {
3127
textRef.current = node;
3228

33-
if (ref === null) {
29+
if (!ref) {
3430
return;
3531
}
3632

@@ -44,41 +40,39 @@ export const LegacyText = forwardRef(
4440
// This is a special case for `Text` component. After https://github.com/software-mansion/react-native-gesture-handler/pull/3379 we check for
4541
// `displayName` field. However, `Text` from RN has this field set to `Text`, but is also present in `RNSVGElements` set.
4642
// We don't want to treat our `Text` as the one from `SVG`, therefore we add special field to ref.
47-
refHandler.rngh = true;
43+
handler.rngh = true;
4844

49-
useEffect(() => {
50-
if (Platform.OS !== 'web') {
51-
return;
52-
}
45+
return handler;
46+
}, [ref]);
5347

54-
const textElement = ref
55-
? (ref as RefObject<React.ComponentRef<typeof RNText>>).current
56-
: textRef.current;
48+
useEffect(() => {
49+
if (Platform.OS !== 'web') {
50+
return;
51+
}
5752

58-
// At this point we are sure that textElement is div in HTML tree
59-
(textElement as unknown as HTMLDivElement)?.setAttribute(
60-
'rnghtext',
61-
'true'
62-
);
63-
}, []);
64-
65-
return onPress || onLongPress ? (
66-
<GestureDetector gesture={native}>
67-
<RNText
68-
onPress={onPress}
69-
onLongPress={onLongPress}
70-
ref={refHandler}
71-
{...rest}
72-
/>
73-
</GestureDetector>
74-
) : (
75-
<RNText ref={ref} {...rest} />
53+
// At this point we are sure that textElement is div in HTML tree
54+
(textRef.current as unknown as HTMLDivElement)?.setAttribute(
55+
'rnghtext',
56+
'true'
7657
);
77-
}
78-
);
58+
}, []);
59+
60+
return onPress || onLongPress ? (
61+
<GestureDetector gesture={native}>
62+
<RNText
63+
onPress={onPress}
64+
onLongPress={onLongPress}
65+
ref={refHandler}
66+
{...rest}
67+
/>
68+
</GestureDetector>
69+
) : (
70+
<RNText ref={refHandler} {...rest} />
71+
);
72+
};
7973

8074
/**
81-
* @deprecated `LegacyText` is deprecated. Since Gesture Handler 3 you can wrap `Text` with `GestureDetector`.
75+
* @deprecated `LegacyText` is deprecated. Since Gesture Handler 3, you should wrap `Text` with `GestureDetector`, `InterceptingGestureDetector`, or `VirtualGestureDetector`.
8276
*/
8377
// eslint-disable-next-line @typescript-eslint/no-redeclare
8478
export type LegacyText = typeof LegacyText & RNText;

0 commit comments

Comments
 (0)