पाठ 18 / 25
Native Modules
When JavaScript is not enough.
Using and writing native modules
Most needs are covered by existing libraries; check the React Native Directory and Expo SDK first, and confirm a library supports the New Architecture. Libraries with native code require a development build, not Expo Go, and in Expo projects many include a config plugin that edits native project settings during prebuild. When you must write native code, the Expo Modules API lets you write modules in Swift and Kotlin with a small declarative DSL (Name, Function, AsyncFunction, Events, View) and works in Expo and bare apps. The core React Native alternative is a Turbo Native Module with a typed spec processed by Codegen. You can scaffold an Expo module with npx create-expo-module.
A tiny Expo module in Swift
Expo Modules API sketch; check the docs for your version.
// ios/BatteryModule.swift (Swift, shown as a comment block)
// import ExpoModulesCore
// public class BatteryModule: Module {
// public func definition() -> ModuleDefinition {
// Name("Battery")
// Function("isLowPowerMode") { () -> Bool in
// ProcessInfo.processInfo.isLowPowerModeEnabled
// }
// }
// }
// src/Battery.ts - calling it from TypeScript
import { requireNativeModule } from "expo-modules-core";
const Battery = requireNativeModule("Battery");
export function isLowPowerMode(): boolean {
return Battery.isLowPowerMode();
}Keep the native surface small
Expose a few well-typed functions and do the platform work natively. A narrow API is easier to keep identical on iOS and Android.
An adapter plug
A native module is a travel adapter: your JavaScript plug stays the same, and the adapter fits it into each country's socket.
त्वरित जाँच: Which tool lets you write native modules in Swift and Kotlin with a declarative DSL?
- The Expo Modules API
- AsyncStorage
- StyleSheet.create
- The Metro bundler config
Answer
The Expo Modules API — Expo modules work in Expo and bare React Native apps.