Skip to content

Commit 82325b4

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
use feature flag in render function for optimized text (facebook#45068)
Summary: Pull Request resolved: facebook#45068 changelog: [internal] In D58672844 I added gating to module.exports. This gating is sensitive to when feature flags are initialised and causes test failures and regressions for developers. Let's move the feature flag check to component's render function. It introduces extra spread operator but it is good enough to compare new and old <Text /> component. Reviewed By: GijsWeterings Differential Revision: D58783941 fbshipit-source-id: f89f4f48e6aeb774ed4a84483a9f4ad59d5bc045
1 parent 51e464f commit 82325b4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

packages/react-native/Libraries/Text/Text.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import processColor from '../StyleSheet/processColor';
1919
import Platform from '../Utilities/Platform';
2020
import TextAncestor from './TextAncestor';
2121
import {NativeText, NativeVirtualText} from './TextNativeComponent';
22+
import TextOptimized from './TextOptimized';
2223
import * as React from 'react';
2324
import {useContext, useMemo, useState} from 'react';
2425

@@ -27,7 +28,7 @@ import {useContext, useMemo, useState} from 'react';
2728
*
2829
* @see https://reactnative.dev/docs/text
2930
*/
30-
const Text: React.AbstractComponent<
31+
const TextLegacy: React.AbstractComponent<
3132
TextProps,
3233
React.ElementRef<typeof NativeText | typeof NativeVirtualText>,
3334
> = React.forwardRef((props: TextProps, forwardedRef) => {
@@ -308,7 +309,7 @@ const Text: React.AbstractComponent<
308309
);
309310
});
310311

311-
Text.displayName = 'Text';
312+
TextLegacy.displayName = 'TextLegacy';
312313

313314
/**
314315
* Returns false until the first time `newValue` is true, after which this will
@@ -338,6 +339,17 @@ const verticalAlignToTextAlignVerticalMap = {
338339
middle: 'center',
339340
};
340341

341-
module.exports = ((ReactNativeFeatureFlags.shouldUseOptimizedText()
342-
? require('./TextOptimized')
343-
: Text): typeof Text);
342+
const Text: React.AbstractComponent<
343+
TextProps,
344+
React.ElementRef<typeof NativeText | typeof NativeVirtualText>,
345+
> = React.forwardRef((props: TextProps, forwardedRef) => {
346+
if (ReactNativeFeatureFlags.shouldUseOptimizedText()) {
347+
return <TextOptimized {...props} ref={forwardedRef} />;
348+
} else {
349+
return <TextLegacy {...props} ref={forwardedRef} />;
350+
}
351+
});
352+
353+
Text.displayName = 'Text';
354+
355+
module.exports = Text;

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8045,7 +8045,7 @@ exports[`public API should not change unintentionally Libraries/Text/Text.js 1`]
80458045
TextProps,
80468046
React.ElementRef<typeof NativeText | typeof NativeVirtualText>,
80478047
>;
8048-
declare module.exports: typeof Text;
8048+
declare module.exports: Text;
80498049
"
80508050
`;
80518051

0 commit comments

Comments
 (0)