From 3154ec8a38e6014090039be54e0ca597fa967fdd Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Fri, 14 Jun 2024 13:37:19 -0400 Subject: [PATCH] Use a constant HostContext in prod builds for Fabric renderer (#29888) ## Summary In the Fabric renderer in React Native, we only use the HostContext to issue soft errors in __DEV__ bundles when attempting to add a raw text child to a node that may not support them. Moving the logic to set this context to __DEV__ bundles only unblocks more expensive methods for resolving whether a parent context supports raw text children, like resolving this information from `getViewConfigForType`. ## How did you test this change? yarn test (--prod) --- .../src/ReactFiberConfigFabric.js | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/packages/react-native-renderer/src/ReactFiberConfigFabric.js b/packages/react-native-renderer/src/ReactFiberConfigFabric.js index cbb85abe7c2dd..be64b37fef257 100644 --- a/packages/react-native-renderer/src/ReactFiberConfigFabric.js +++ b/packages/react-native-renderer/src/ReactFiberConfigFabric.js @@ -132,6 +132,8 @@ export function appendInitialChild( appendChildNode(parentInstance.node, child.node); } +const PROD_HOST_CONTEXT: HostContext = {isInAParentText: true}; + export function createInstance( type: string, props: Props, @@ -220,29 +222,35 @@ export function finalizeInitialChildren( export function getRootHostContext( rootContainerInstance: Container, ): HostContext { - return {isInAParentText: false}; + if (__DEV__) { + return {isInAParentText: false}; + } + + return PROD_HOST_CONTEXT; } export function getChildHostContext( parentHostContext: HostContext, type: string, ): HostContext { - const prevIsInAParentText = parentHostContext.isInAParentText; - const isInAParentText = - type === 'AndroidTextInput' || // Android - type === 'RCTMultilineTextInputView' || // iOS - type === 'RCTSinglelineTextInputView' || // iOS - type === 'RCTText' || - type === 'RCTVirtualText'; - - // TODO: If this is an offscreen host container, we should reuse the - // parent context. - - if (prevIsInAParentText !== isInAParentText) { - return {isInAParentText}; - } else { - return parentHostContext; + if (__DEV__) { + const prevIsInAParentText = parentHostContext.isInAParentText; + const isInAParentText = + type === 'AndroidTextInput' || // Android + type === 'RCTMultilineTextInputView' || // iOS + type === 'RCTSinglelineTextInputView' || // iOS + type === 'RCTText' || + type === 'RCTVirtualText'; + + // TODO: If this is an offscreen host container, we should reuse the + // parent context. + + if (prevIsInAParentText !== isInAParentText) { + return {isInAParentText}; + } } + + return parentHostContext; } export function getPublicInstance(instance: Instance): null | PublicInstance {