diff --git a/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js b/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js index 7e69231a9a2..259571e1683 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js +++ b/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js @@ -127,6 +127,10 @@ export default NativeComponentRegistry.get(nativeComponentName, () => VIEW_CONFI `.trim(); }; +// If static view configs are enabled, get whether the native component exists +// in the app binary using hasViewManagerConfig() instead of getViewManagerConfig(). +// Old getViewManagerConfig() checks for the existance of the native Paper view manager. +// New hasViewManagerConfig() queries Fabric’s native component registry directly. const DeprecatedComponentNameCheckTemplate = ({ componentName, paperComponentNameDeprecated, @@ -135,12 +139,23 @@ const DeprecatedComponentNameCheckTemplate = ({ paperComponentNameDeprecated: string, }) => ` -if (UIManager.getViewManagerConfig('${componentName}')) { - nativeComponentName = '${componentName}'; -} else if (UIManager.getViewManagerConfig('${paperComponentNameDeprecated}')) { - nativeComponentName = '${paperComponentNameDeprecated}'; +const staticViewConfigsEnabled = global.__fbStaticViewConfig === true; +if (staticViewConfigsEnabled) { + if (UIManager.hasViewManagerConfig('${componentName}')) { + nativeComponentName = '${componentName}'; + } else if (UIManager.hasViewManagerConfig('${paperComponentNameDeprecated}')) { + nativeComponentName = '${paperComponentNameDeprecated}'; + } else { + throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}", with SVC enabled.'); + } } else { - throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}"'); + if (UIManager.getViewManagerConfig('${componentName}')) { + nativeComponentName = '${componentName}'; + } else if (UIManager.getViewManagerConfig('${paperComponentNameDeprecated}')) { + nativeComponentName = '${paperComponentNameDeprecated}'; + } else { + throw new Error('Failed to find native component for either "${componentName}" or "${paperComponentNameDeprecated}", with SVC disabled.'); + } } `.trim(); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap index 2978af1e757..b4975e0d639 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap +++ b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap @@ -973,12 +973,23 @@ const NativeComponentRegistry = require('react-native/Libraries/NativeComponent/ const {UIManager} = require(\\"react-native\\") let nativeComponentName = 'NativeComponentName'; -if (UIManager.getViewManagerConfig('NativeComponentName')) { - nativeComponentName = 'NativeComponentName'; -} else if (UIManager.getViewManagerConfig('DeprecatedNativeComponentName')) { - nativeComponentName = 'DeprecatedNativeComponentName'; +const staticViewConfigsEnabled = global.__fbStaticViewConfig === true; +if (staticViewConfigsEnabled) { + if (UIManager.hasViewManagerConfig('NativeComponentName')) { + nativeComponentName = 'NativeComponentName'; + } else if (UIManager.hasViewManagerConfig('DeprecatedNativeComponentName')) { + nativeComponentName = 'DeprecatedNativeComponentName'; + } else { + throw new Error('Failed to find native component for either \\"NativeComponentName\\" or \\"DeprecatedNativeComponentName\\", with SVC enabled.'); + } } else { - throw new Error('Failed to find native component for either \\"NativeComponentName\\" or \\"DeprecatedNativeComponentName\\"'); + if (UIManager.getViewManagerConfig('NativeComponentName')) { + nativeComponentName = 'NativeComponentName'; + } else if (UIManager.getViewManagerConfig('DeprecatedNativeComponentName')) { + nativeComponentName = 'DeprecatedNativeComponentName'; + } else { + throw new Error('Failed to find native component for either \\"NativeComponentName\\" or \\"DeprecatedNativeComponentName\\", with SVC disabled.'); + } } export default NativeComponentRegistry.get(nativeComponentName, () => ({ uiViewClassName: 'NativeComponentName',