Files
2nd/10_Wiki/Topics/Frontend/TurboModules.md
T
Antigravity Agent f8b21af4be Wiki cleanup: error-doc removal, dedup merge, link normalization
10_Wiki/Topics 대규모 정리:
- 오류 캡처/미완성 stub 문서 227개 제거
- 교차폴더 중복 43클러스터 병합 (63파일 → redirect)
- 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건
- 카테고리 MOC 6개 신규 생성
- Graph 섹션 미해결 related-keyword 링크 10,058건 제거

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 23:52:15 +09:00

171 lines
5.0 KiB
Markdown

---
id: wiki-2026-0508-turbomodules
title: TurboModules
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [React Native TurboModules, RN New Architecture Modules]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [react-native, jsi, native-modules, new-architecture]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: typescript
framework: react-native
---
# TurboModules
## 매 한 줄
> **"매 JSI-direct 의 type-safe native module"**. TurboModules 의 React Native 의 New Architecture 의 native-side API 의 JS 의 expose, 매 legacy bridge (JSON serialize) 의 X 의 JSI 의 direct C++ call, 매 codegen 의 type safety. 매 2026: RN 0.76+ 의 New Architecture 의 default — TurboModules + Fabric 의 baseline.
## 매 핵심
### 매 legacy NativeModule 의 차이
- **Legacy**: async-only, JSON serialize, 매 batched message queue, runtime type check.
- **TurboModule**: sync 또는 async, JSI direct call, lazy load, codegen-typed (Flow/TS spec).
- **Perf**: 매 startup 의 lazy load 의 win, 매 call 의 serialize overhead 의 zero.
### 매 codegen flow
1. JS spec file (`Native<Name>.ts`) 의 author — 매 TurboModule interface.
2. `react-native codegen` 의 ObjC++ / Java header 의 generate.
3. Native impl 의 generated header 의 conform.
4. JS 의 `TurboModuleRegistry.getEnforcing<Spec>('Name')` 의 access.
### 매 응용
1. Custom native sensor / Bluetooth API.
2. Heavy native compute (image filter, ML preprocess) 의 JSI sync 의 expose.
3. Vendor SDK wrapping (payment, analytics).
4. Library author 의 RN 0.76+ 의 publish.
## 💻 패턴
### Spec file (TS)
```typescript
// specs/NativeMathLib.ts
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
export interface Spec extends TurboModule {
add(a: number, b: number): number;
multiplyAsync(a: number, b: number): Promise<number>;
getDeviceInfo(): { model: string; os: string };
}
export default TurboModuleRegistry.getEnforcing<Spec>('MathLib');
```
### Codegen config (package.json)
```json
{
"name": "react-native-mathlib",
"codegenConfig": {
"name": "RNMathLibSpec",
"type": "modules",
"jsSrcsDir": "specs",
"android": { "javaPackageName": "com.mathlib" }
}
}
```
### iOS impl (ObjC++)
```objcpp
// MathLib.mm
#import "MathLib.h"
#import <RNMathLibSpec/RNMathLibSpec.h>
@implementation MathLib
RCT_EXPORT_MODULE()
- (NSNumber *)add:(double)a b:(double)b {
return @(a + b);
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params {
return std::make_shared<facebook::react::NativeMathLibSpecJSI>(params);
}
@end
```
### Android impl (Kotlin)
```kotlin
class MathLibModule(reactContext: ReactApplicationContext) :
NativeMathLibSpec(reactContext) {
override fun getName() = "MathLib"
override fun add(a: Double, b: Double): Double = a + b
override fun multiplyAsync(a: Double, b: Double, promise: Promise) {
promise.resolve(a * b)
}
}
```
### JS consumption
```typescript
import MathLib from './specs/NativeMathLib';
const sum = MathLib.add(2, 3); // 매 sync — 매 JSI direct
const product = await MathLib.multiplyAsync(4, 5);
```
### Enable New Architecture
```bash
# 매 RN 0.76+ default — 매 0.74 의 opt-in
# iOS:
RCT_NEW_ARCH_ENABLED=1 pod install
# Android: gradle.properties
newArchEnabled=true
```
### Lazy access guard
```typescript
const MathLib = TurboModuleRegistry.get<Spec>('MathLib');
if (MathLib == null) {
throw new Error('MathLib not linked');
}
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| 신규 RN library (2026) | TurboModule + codegen |
| Legacy 0.68 below 의 maintain | NativeModule (deprecated path) |
| Sync native call 의 require | TurboModule (legacy 의 X) |
| UI component (not API) | Fabric component (sibling 의 New Arch) |
| Cross-platform native | TurboModule + shared C++ via JSI |
**기본값**: 매 RN 0.76+ 의 모든 native module 의 TurboModule.
## 🔗 Graph
- 부모: [[React Native]] · [[React Native New Architecture]]
- 변형: [[Fabric Renderer]] · [[JSI]]
- Adjacent: [[Codegen]] · [[Hermes]]
## 🤖 LLM 활용
**언제**: 매 RN 0.76+ 의 native module 의 author 또는 migrate.
**언제 X**: 매 expo-managed app — 매 prebuild 또는 config plugin 의 prefer.
## ❌ 안티패턴
- **Spec file 의 codegen 의 skip 의 manual header**: 매 type drift — 매 always codegen.
- **Sync TurboModule 의 long-running task**: 매 JS thread block — 매 async 의 use.
- **Bridge module 의 New Arch 의 mix**: 매 deprecation warning + perf loss.
- **`getEnforcing` 없 의 unchecked access**: 매 null reference 의 silent fail.
- **C++ TurboModule 의 type 의 manual marshal**: 매 codegen 의 auto-handle 의 ignore.
## 🧪 검증 / 중복
- Verified (RN 0.76+ docs, reactnative.dev/docs/the-new-architecture).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — codegen, JSI direct, iOS/Android impl |