React Native · Mobile UI at native speed
How do you write a native module?
Short answer
Define a typed spec, generate glue code with codegen, and implement the module in Swift or Kotlin. TurboModules load lazily and use JSI, so expose a narrow asynchronous API with explicit error handling.
A native module bridges a platform capability that JavaScript cannot access directly, such as battery state, Bluetooth or a proprietary SDK. In the new architecture you write a TypeScript spec and let codegen produce the native interfaces.
import type { TurboModule } from "react-native";
import { TurboModuleRegistry } from "react-native";
export interface Spec extends TurboModule {
getBatteryLevel(): Promise<number>;
isLowPowerMode(): boolean;
}
export default TurboModuleRegistry.getEnforcing<Spec>("BatteryInfo");Implementation steps
- Create the spec under a directory listed in codegenConfig
- Run codegen and implement the generated protocol in Swift or Kotlin
- Register the module with the app package or autolinking
- Keep methods small and return promises or use events for long work
- Handle errors with typed codes rather than throwing raw strings
Do not do heavy work synchronously on the JS call. A synchronous method blocks JavaScript, and a long native operation should return a promise or emit progress events. Also avoid passing large blobs across the boundary when a file URI or shared buffer would do.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between a TurboModule and a legacy native module?
- 2When should work be synchronous versus asynchronous?
- 3How does codegen know which files to process?
- 4Why can't native module changes ship over the air?
Say it in the interview
I start with a small TypeScript spec describing the methods and events, then let codegen generate the native interfaces and implement them in Swift or Kotlin. I keep the surface narrow, prefer promises for long work and avoid synchronous calls that block JavaScript. I also treat it as a native change, so it needs a store build rather than an over-the-air update.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live React Native mock interview and get scored feedback on depth, clarity and confidence.