diff --git a/packages/react-native/Libraries/Utilities/__tests__/codegenNativeComponent-test.js b/packages/react-native/Libraries/Utilities/__tests__/codegenNativeComponent-test.js index 4efe80cc09d..a20f705a1ae 100644 --- a/packages/react-native/Libraries/Utilities/__tests__/codegenNativeComponent-test.js +++ b/packages/react-native/Libraries/Utilities/__tests__/codegenNativeComponent-test.js @@ -29,6 +29,14 @@ jest ); describe('codegenNativeComponent', () => { + beforeEach(() => { + global.RN$Bridgeless = false; + jest + .spyOn(console, 'warn') + .mockReset() + .mockImplementation(() => {}); + }); + it('should require component as is ', () => { const component = codegenNativeComponent('ComponentName'); expect(component).toBe('ComponentName'); @@ -64,4 +72,18 @@ describe('codegenNativeComponent', () => { 'Failed to find native component for either ComponentNameDoesNotExistOne or ComponentNameDoesNotExistTwo', ); }); + + it('should NOT warn if called directly in BRIDGE mode', () => { + global.RN$Bridgeless = false; + codegenNativeComponent('ComponentName'); + expect(console.warn).not.toHaveBeenCalled(); + }); + + it('should warn if called directly in BRIDGELESS mode', () => { + global.RN$Bridgeless = true; + codegenNativeComponent('ComponentName'); + expect(console.warn).toHaveBeenCalledWith( + `Codegen didn't run for ComponentName. This will be an error in the future. Make sure you are using @react-native/babel-preset when building your JavaScript code.`, + ); + }); }); diff --git a/packages/react-native/Libraries/Utilities/codegenNativeComponent.js b/packages/react-native/Libraries/Utilities/codegenNativeComponent.js index 3f60697155e..3f9187f8fe1 100644 --- a/packages/react-native/Libraries/Utilities/codegenNativeComponent.js +++ b/packages/react-native/Libraries/Utilities/codegenNativeComponent.js @@ -35,12 +35,10 @@ function codegenNativeComponent( componentName: string, options?: Options, ): NativeComponentType { - if (global.RN$Bridgeless === true) { - const errorMessage = - "Native Component '" + - componentName + - "' that calls codegenNativeComponent was not code generated at build time. Please check its definition."; - console.error(errorMessage); + if (global.RN$Bridgeless === true && __DEV__) { + console.warn( + `Codegen didn't run for ${componentName}. This will be an error in the future. Make sure you are using @react-native/babel-preset when building your JavaScript code.`, + ); } let componentNameInUse =