Skip to content

Lesson 2 of 9 · 28m

Native Modules Without Mysticism

Design TurboModules, Expo modules, and platform-specific implementations with explicit ownership, threading, error surfaces, and testability.

The first native module most teams write is born in panic: a payment SDK, Bluetooth peripheral, document scanner, or biometric API does not fit inside JavaScript. The mistake is treating the module as a thin emergency tunnel. Six months later nobody knows which thread it uses, which errors are fatal, or whether Android and iOS implement the same contract. Native modules deserve interface design, not improvisation.

Write the contract before the platform code

Start with the JavaScript-facing API and document exactly what it promises:

export type ReceiptScan = {
  merchant: string | null
  totalCents: number | null
  confidence: number
}

export interface ReceiptScannerModule {
  scan(uri: string): Promise<ReceiptScan>
  cancel(): Promise<void>
}

Keep primitive, serializable values at the boundary. Strings, numbers, booleans, arrays, and flat records are easier to debug than deeply nested platform objects. Do not leak Android Bitmap, iOS UIImage, native view handles, or callback lifecycles unless you are prepared to own them.

TurboModules are generated contracts

Modern React Native uses codegen to create type-safe interfaces. A specification might live in src/NativeReceiptScanner.ts:

import type {TurboModule} from 'react-native'
import {TurboModuleRegistry} from 'react-native'

export interface Spec extends TurboModule {
  scan(uri: string): Promise<ReceiptScan>
  cancel(): Promise<void>
}

export default TurboModuleRegistry.getEnforcing<Spec>('NativeReceiptScanner')

The key benefit is not convenience; it is a compile-time shared contract between JavaScript and native implementations. In Expo apps, the Expo Modules API is often the pragmatic alternative because it gives consistent Kotlin and Swift structure with less boilerplate.

Threading is part of the API

A native module that blocks the wrong thread becomes a frame-drop generator. CPU-heavy scanning, hashing, image processing, and compression belong off the UI thread. JavaScript is not automatically parallel either; if native code emits progress events 120 times per second, you can recreate the original bottleneck at the boundary.

Good contract: start job -> native worker processes -> single completion/result event
Risky contract: native emits uncompressed progress callbacks every frame

Errors need the same discipline. JavaScript should distinguish user cancellation, permission denial, invalid input, unsupported hardware, retryable failure, and programmer error. Never collapse every failure into Error: unknown.

Test the boundary at three levels

Unit-test platform logic in Kotlin or Swift. Integration-test the module contract in the app. Then manually test permission transitions, backgrounding, interruption, rotation, low memory, and airplane mode. Native modules fail at lifecycle edges far more often than happy paths.

Field notes

  • Every native capability becomes upgrade debt when React Native, Xcode, Android Gradle Plugin, or the SDK vendor changes.
  • Keep Android and iOS method names, result shapes, units, and cancellation semantics identical unless there is a real platform distinction.
  • A synchronous TurboModule is powerful and dangerous: blocking it blocks JavaScript execution.
  • Log native module version and implementation in diagnostics; mixed builds are common during staged rollouts.

Was this lesson helpful?

Sign in to rate