Skip to main content

Module constants (old arch)

info

You may notice, that JS specs contain codegen-related methods, classes, types, etc. to make things more future-proof.

That's because:

  • those elements are available since RN older versions (even from v0.65)
  • those elements are falling back to "old architecture" implementation (e.g. codegenNativeComponent)
  • it introduces type safety for exposed native parts on JS side
  • it's much easier to keep single specification on JS side - when old arch will be dropped, there'll be no need to change anything on JS side

So to make it easier, let's use them, to get you more familiar with it 👍

Constants declaration

src/NativeMyAwesomeModule.ts
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

// ...

export interface Spec extends TurboModule {
getConstants(): { ALONSO: string, HAMILTON: string, LATIFI: string, VERSTAPPEN: string }
}

// TurboModuleRegistry uses NativeModules['MyAwesomeModule'] on old arch
export default TurboModuleRegistry.getEnforcing<Spec>('MyAwesomeModule');