0Pricing
React Native Academy · บทเรียน

โมดูลเนทีฟ Turbo ด้วย JSI

กำหนดไฟล์ข้อกำหนด TypeScript สำหรับโมดูลเนทีฟ Turbo นำไปใช้ใน Kotlin และ Swift ด้วยอินเทอร์เฟซที่สร้างจาก codegen และเรียกโมดูลแบบพร้อมกันผ่าน JSI

โมดูลเนทีฟ Turbo ด้วย JSI เป็นบทเรียน React Native Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน React Native Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Problem with the Legacy Bridge

The legacy React Native bridge serializes every message between JavaScript and native into JSON, then deserializes it on the other side. This round-trip adds latency for every native call and blocks threads while serialization happens. For frequent calls like scroll events or animations, this overhead causes frame drops. JSI (JavaScript Interface) was created to eliminate this bottleneck.

What Is JSI

JSI (JavaScript Interface) is a lightweight C++ layer that lets JavaScript hold direct references to C++ host objects. Instead of sending a JSON-serialized message across the bridge, a JS call invokes a C++ function pointer directly and synchronously. This means native calls can be synchronous, zero-copy, and orders of magnitude faster than bridge-based calls.

Turbo Modules Architecture Overview

Turbo Native Modules are the new module system built on JSI. Key differences from legacy modules include: lazy loading (modules initialize only when first accessed instead of all at startup), typed interfaces (generated from a TypeScript spec so types are guaranteed on both sides), and synchronous calls possible without blocking the JS thread via JSI. The codegen tool generates the boilerplate C++ and native glue code automatically.

Writing the TypeScript Spec File

A Turbo Module starts with a TypeScript spec file that declares the module's interface. This file lives in your project and is processed by react-native-codegen to generate native interface files. The spec must import TurboModuleRegistry and define a type that extends TurboModule.

// NativeDeviceInfo.ts (spec file)
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
  getDeviceModel(): string;
  getBatteryLevel(): Promise<number>;
  addListener(eventType: string): void;
  removeListeners(count: number): void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('DeviceInfo');

Running Codegen to Generate Native Code

After writing the TypeScript spec, you run React Native Codegen to generate the native interface files. For managed Expo projects, this happens automatically during expo prebuild. For bare React Native projects, run yarn react-native codegen. Codegen outputs Swift/Kotlin interfaces and C++ glue code in the build/generated folder that your native implementation must conform to.

# Bare React Native — run codegen manually
cd android && ./gradlew generateCodegenArtifactsFromSchema

# Then check generated files
ls android/app/build/generated/source/codegen/jni/
# NativeDeviceInfoJSI-generated.cpp
# NativeDeviceInfoJSI.h

Implementing the Module in Kotlin

The Kotlin implementation must extend the codegen-generated abstract class (e.g., NativeDeviceInfoSpec) and override each method declared in the spec. Because the signatures are generated from TypeScript, there is no type mismatch between JS and native — the codegen enforces them at build time rather than at runtime.

// DeviceInfoModule.kt
package com.yourapp

import com.facebook.react.bridge.ReactApplicationContext
import com.yourapp.NativeDeviceInfoSpec

class DeviceInfoModule(context: ReactApplicationContext) :
    NativeDeviceInfoSpec(context) {

    companion object {
        const val NAME = 'DeviceInfo'
    }

    override fun getName(): String = NAME

    override fun getDeviceModel(): String {
        return android.os.Build.MODEL
    }
}

Implementing the Module in Swift

The Swift implementation conforms to the codegen-generated protocol (e.g., NativeDeviceInfoSpec). Unlike legacy modules, there is no separate bridge .m file needed for Turbo Modules — the JSI layer handles the binding automatically once the module is registered. The protocol guarantees that the Swift method signatures match the TypeScript spec exactly.

// DeviceInfoModule.swift
import Foundation

@objc(DeviceInfoModule)
class DeviceInfoModule: NSObject, NativeDeviceInfoSpec {

  func getDeviceModel() -> String {
    return UIDevice.current.model
  }

  func getBatteryLevel(
    _ resolve: @escaping RCTPromiseResolveBlock,
    reject: @escaping RCTPromiseRejectBlock
  ) {
    UIDevice.current.isBatteryMonitoringEnabled = true
    resolve(UIDevice.current.batteryLevel * 100)
  }
}

Synchronous Calls via JSI

One of the biggest JSI benefits is the ability to call native synchronously from JS. When a Turbo Module method returns a non-Promise type (like string or number in the spec), JSI can return the value immediately without scheduling an async callback. This unlocks patterns like reading cached values synchronously that were impossible with the legacy bridge.

// TypeScript spec — synchronous return type
export interface Spec extends TurboModule {
  getDeviceModel(): string;  // synchronous
  getBatteryLevel(): Promise<number>;  // still async
}

// Usage in JS — getDeviceModel() is synchronous
import NativeDeviceInfo from './NativeDeviceInfo';

const model = NativeDeviceInfo.getDeviceModel(); // no await needed
console.log('Model:', model);

Enabling the New Architecture

Turbo Modules require the New Architecture to be enabled in your project. For Android, set newArchEnabled=true in android/gradle.properties. For iOS, set RCT_NEW_ARCH_ENABLED=1 in the Podfile and run pod install. Expo SDK 50+ enables the new architecture automatically for managed workflow projects using the latest config.

# android/gradle.properties
newArchEnabled=true

# ios/Podfile — add before use_react_native!
ENV['RCT_NEW_ARCH_ENABLED'] = '1'

# Then reinstall pods
cd ios && pod install

Registering a Turbo Module

Turbo Modules are registered via a TurboModuleManagerDelegate instead of a ReactPackage. On Android you modify MainApplicationTurboModuleManagerDelegate.kt; on iOS you modify RCTAppDelegate.mm. The codegen also generates a ModuleProvider that wires up the module automatically when you follow the standard naming conventions.

// Android: MainApplicationTurboModuleManagerDelegate.kt
override fun getModule(
    reactApplicationContext: ReactApplicationContext,
    name: String
): NativeModule? {
    return when (name) {
        DeviceInfoModule.NAME -> DeviceInfoModule(reactApplicationContext)
        else -> null
    }
}

Comparing Legacy vs Turbo Module Performance

Benchmarks show Turbo Modules can be 2–3x faster for high-frequency calls compared to legacy bridge modules. The improvement is most noticeable for calls made in rapid succession — such as gesture handlers or animation drivers. For infrequent calls like camera setup, the difference is negligible. Migrate to Turbo Modules when performance-critical native code needs to keep up with 60fps interactions.

// Performance benchmark pattern
const ITERATIONS = 10000;
const start = Date.now();

for (let i = 0; i < ITERATIONS; i++) {
  // Synchronous JSI call
  const model = NativeDeviceInfo.getDeviceModel();
}

const elapsed = Date.now() - start;
console.log('10k JSI calls in', elapsed, 'ms');
// Turbo: ~8ms  vs  Legacy bridge: ~80ms

Quick Check

Test your understanding of React Native Mobile Development concepts from this lesson.

Lesson Recap

In this lesson you learned: how JSI eliminates JSON serialization overhead from the legacy bridge, how to write a TypeScript spec file that codegen uses to generate native interfaces, and how to implement the generated spec in both Kotlin and Swift. You also saw how to enable the New Architecture and the performance benefits of synchronous JSI calls. Next up we explore async callbacks, promises, and native events.

คำถามที่พบบ่อย

บทเรียน “โมดูลเนทีฟ Turbo ด้วย JSI” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โมดูลเนทีฟ Turbo ด้วย JSI” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส React Native Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โมดูลเนทีฟ Turbo ด้วย JSI”

กำหนดไฟล์ข้อกำหนด TypeScript สำหรับโมดูลเนทีฟ Turbo นำไปใช้ใน Kotlin และ Swift ด้วยอินเทอร์เฟซที่สร้างจาก codegen และเรียกโมดูลแบบพร้อมกันผ่าน JSI คุณปฏิบัติ React Native Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน React Native Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน React Native Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “โมดูลเนทีฟ Turbo ด้วย JSI” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน React Native Academy นี้ได้ไหม

ได้ บทเรียน React Native Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเขียนโมดูลเนทีฟแบบเดิมใน Kotlin
  2. การเขียนโมดูลเนทีฟแบบเดิมใน Swift
  3. โมดูลเนทีฟ Turbo ด้วย JSI
  4. ตัวเรียกกลับแบบอะซิงโครนัส พรอมิส และเหตุการณ์จากเนทีฟ
← กลับไปที่ React Native Academy