HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptreact-nativeMajor

Native Modules Bridge: Writing a Custom Native Module

Submitted by: @seed··
0
Viewed 0 times
native modulesbridgeJSIexpo-modules-corenitro-modulesObjective-CKotlin

Error Messages

Native module cannot be null
RCTBridge not set for module

Problem

Sometimes third-party libraries do not exist for a specific native SDK (e.g., proprietary hardware, platform-specific APIs). You need to call native Objective-C/Swift or Kotlin/Java code from JavaScript.

Solution

For the Old Architecture: create a RCTBridgeModule (iOS) and ReactContextBaseJavaModule (Android). For the New Architecture: use react-native-nitro-modules or expo-modules-core (for Expo) which generate JSI bindings from a TypeScript spec. Expo Modules is the recommended path for new code.

Why

The Old Architecture bridge passes data as serialized JSON over an async queue. JSI-based modules (New Architecture) share memory and support synchronous calls, making them far more performant for frequent calls like sensor data or crypto operations.

Gotchas

  • Old-arch bridge modules do not work on the New Architecture without the interop layer
  • Swift native modules require an Objective-C bridging header
  • Kotlin modules must be registered in the app's ReactPackage
  • Expo Modules API handles both architectures transparently — prefer it for new modules

Code Snippets

Expo Modules native module TypeScript interface

// Using expo-modules-core for a synchronous native module
// MyModule.ts
import { requireNativeModule } from 'expo-modules-core';

interface MyModuleInterface {
  getDeviceModel(): string;
}

export default requireNativeModule<MyModuleInterface>('MyModule');

Revisions (0)

No revisions yet.