Skip to the page

How React Native runs your app · mid

TurboModules and Codegen

A TurboModule is a native module built on JSI, described by a typed spec file whose name starts with Native, created lazily the first time JS uses it, and connected to native code through interfaces that Codegen generates from that spec at build time.

The notes

What you must know

  • Spec: specs/NativeLocalStorage.ts with interface Spec extends TurboModule { … }, exported with TurboModuleRegistry.getEnforcing<Spec>('NativeLocalStorage').
  • getEnforcing throws if the module is missing; get returns null instead.
  • codegenConfig in package.json: name, type ("modules" or "components"), jsSrcsDir, android.javaPackageName, ios.modulesProvider.
  • When Codegen runs: on Android as the Gradle task generateCodegenArtifactsFromSchema (also during a normal build); on iOS during pod install. Do not commit generated code.
  • Codegen generates the abstract spec class / protocol and the JSI glue. You still write the native implementation.
  • Lazy loading: the old architecture created every native module at startup; TurboModules are created on first access, which helps startup time.
  • Type safety: if the spec and the native code disagree, the build fails instead of crashing at runtime.
  • 0.87 removed the useTurboModules flag: TurboModules are simply always on.

The red pen

Common traps: mistake → fix

  • Mistake: "Codegen writes my native code"

    Fix: it writes interfaces and glue; you implement the methods.

  • Mistake: "Any file name works for a spec"

    Fix: module specs need the Native prefix; component specs need the NativeComponent suffix.

  • Mistake: "TurboModules make native work free"

    Fix: a sync method still blocks the JS thread while it runs.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is a TurboModule and how is it different from a legacy NativeModule?
  2. What does lazy loading buy you at startup?
  3. Why does the New Architecture need Codegen? What do you write and what is generated?
  4. When does Codegen run on Android and on iOS?
  5. What happens if the spec and the native implementation drift apart?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview