### Example Folder Structure for RTNCalculator Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules.md This example demonstrates the recommended folder structure for a React Native Turbo Native Module project, named RTNCalculator. It includes separate directories for JavaScript, iOS, and Android implementations, promoting modularity and eventual open-source release. ```sh TurboModulesGuide ├── MyApp └── RTNCalculator ├── android ├── ios └── js ``` -------------------------------- ### Create New React Native Project Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md Initializes a new React Native project with the latest version, skipping the initial installation to allow for subsequent setup steps. ```shell npx react-native@latest init AwesomeProject --skip-install cd AwesomeProject yarn install ``` -------------------------------- ### Initialize React Native App and Enable New Architecture Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules-xplat.md Commands to create a new React Native application, install dependencies, and enable the New Architecture for Android and iOS. The Android configuration is done via `gradle.properties`, and iOS requires specific `pod install` flags. ```sh npx react-native init CxxTurboModulesGuide cd CxxTurboModulesGuide yarn install # On Android, modify android/gradle.properties: # - newArchEnabled=false # + newArchEnabled=true # On iOS, run pod install with the flag: # RCT_NEW_ARCH_ENABLED=1 bundle exec pod install ``` -------------------------------- ### Example: Generate iOS Native Code Artifacts Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/appendix.md An example demonstrating the manual invocation of the Codegen scripts to generate native iOS interface code. It first generates a schema file from JavaScript sources located in './js' and then uses this schema to create native artifacts in the './ios' directory for a library named 'MyLibSpecs', specifically for modules. ```bash # Generate schema - only needs to be done whenever JS specs change node node_modules/react-native-codegen/lib/cli/combine/combine-js-to-schema-cli.js /tmp/schema.json ./js # Generate native code artifacts node node_modules/react-native/scripts/generate-specs-cli.js \ --platform ios \ --schemaPath /tmp/schema.json \ --outputDir ./ios \ --libraryName MyLibSpecs \ --libraryType modules ``` -------------------------------- ### Run Codegen CLI (Default) Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/codegen.md This example shows how to run the React Native Codegen CLI with default settings. It reads configuration from the current directory's package.json. ```sh npx react-native codegen ``` -------------------------------- ### Implement CalculatorPackage for React Native (Java/Kotlin) Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules.md This class implements the BaseReactPackage interface, which is essential for React Native to recognize and utilize native modules. An empty implementation is sufficient for Codegen. ```java package com.rtncalculator; import androidx.annotation.Nullable; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.module.model.ReactModuleInfoProvider; import com.facebook.react.BaseReactPackage; import java.util.Collections; import java.util.List; public class CalculatorPackage extends BaseReactPackage { @Nullable @Override public NativeModule getModule(String name, ReactApplicationContext reactContext) { return null; } @Override public ReactModuleInfoProvider getReactModuleInfoProvider() { return null; } } ``` ```kotlin package com.rtncalculator; import com.facebook.react.BaseReactPackage import com.facebook.react.bridge.NativeModule import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.module.model.ReactModuleInfoProvider class CalculatorPackage : BaseReactPackage() { override fun getModule(name: String?, reactContext: ReactApplicationContext): NativeModule? = null override fun getReactModuleInfoProvider(): ReactModuleInfoProvider? = null } ``` -------------------------------- ### Add `pod-install` Script to package.json Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md Adds a script to `package.json` for easily running `pod install` with the New Architecture enabled for iOS. This simplifies the process of updating native dependencies. ```json "scripts": { "pod-install": "cd ios && RCT_NEW_ARCH_ENABLED=1 bundle exec pod install" } ``` -------------------------------- ### Configure build.gradle for Android (Java/Kotlin) Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules.md This Gradle configuration sets up the Android build environment for React Native, specifying dependencies and SDK versions. It's applicable for both Java and Kotlin projects. ```java buildscript { ext.safeExtGet = {prop, fallback -> rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback } repositories { google() gradlePluginPortal() } dependencies { classpath("com.android.tools.build:gradle:7.3.1") } } apply plugin: 'com.android.library' apply plugin: 'com.facebook.react' android { compileSdkVersion safeExtGet('compileSdkVersion', 33) namespace "com.rtncalculator" } repositories { mavenCentral() google() } dependencies { implementation 'com.facebook.react:react-native' } ``` ```kotlin buildscript { ext.safeExtGet = {prop, fallback -> rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback } repositories { google() gradlePluginPortal() } dependencies { classpath("com.android.tools.build:gradle:7.3.1") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.22") } } apply plugin: 'com.android.library' apply plugin: 'com.facebook.react' apply plugin: 'org.jetbrains.kotlin.android' android { compileSdkVersion safeExtGet('compileSdkVersion', 33) namespace "com.rtncalculator" } repositories { mavenCentral() google() } dependencies { implementation 'com.facebook.react:react-native' } ``` -------------------------------- ### Enable New Architecture for iOS Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md Enables the New Architecture for iOS by running `bundle install` and `pod install` with the `RCT_NEW_ARCH_ENABLED=1` flag. This is for apps created from the React Native CLI. ```shell # from `ios` directory bundle install && RCT_NEW_ARCH_ENABLED=1 bundle exec pod install ``` -------------------------------- ### Shared package.json Configuration for React Native Module Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/fabric-native-components.md Defines the metadata, dependencies, and codegen configuration for a React Native module. It is used by yarn for installation and by Codegen for native component setup. Ensure placeholders like GitHub handle and author details are updated. ```json { "name": "rtn-centered-text", "version": "0.0.1", "description": "Showcase a Fabric Native Component with a centered text", "react-native": "js/index", "source": "js/index", "files": [ "js", "android", "ios", "rtn-centered-text.podspec", "!android/build", "!ios/build", "!**/__tests__", "!**/__fixtures__", "!**/__mocks__" ], "keywords": ["react-native", "ios", "android"], "repository": "https://github.com//rtn-centered-text", "author": " (https://github.com/)", "license": "MIT", "bugs": { "url": "https://github.com//rtn-centered-text/issues" }, "homepage": "https://github.com//rtn-centered-text#readme", "devDependencies": {}, "peerDependencies": { "react": "*", "react-native": "*" }, "codegenConfig": { "name": "RTNCenteredTextSpecs", "type": "components", "jsSrcsDir": "js" } } ``` -------------------------------- ### Migrating dispatchViewManagerCommand with codegenNativeCommands (React Native) Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-libraries-prerequisites.md This example illustrates how to migrate away from `UIManager.dispatchViewManagerCommand` by using `codegenNativeCommands`. It defines a `NativeCommands` interface and uses it with `codegenNativeCommands` to generate the necessary command dispatching logic for a custom map component. ```js // @flow strict-local import codegenNativeCommands from "react-native/Libraries/Utilities/codegenNativeCommands"; import type { HostComponent } from "react-native/Libraries/Renderer/shims/ReactNativeTypes"; type MyCustomMapNativeComponentType = HostComponent; interface NativeCommands { +moveToRegion: ( viewRef: React.ElementRef, region: MapRegion, duration: number ) => void; } export const Commands: NativeCommands = codegenNativeCommands({ supportedCommands: ["moveToRegion"], }); ``` -------------------------------- ### Enable New Architecture for Existing iOS Apps Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md Reinstalls pods for an existing iOS app with the New Architecture enabled by running `pod install` using the `RCT_NEW_ARCH_ENABLED=1` flag. ```shell # Run pod install with the flag: RCT_NEW_ARCH_ENABLED=1 bundle exec pod install ``` -------------------------------- ### package.json Configuration for React Native New Architecture Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules.md This JSON file defines the metadata, dependencies, and Codegen configuration for a React Native module. It includes package details, peer dependencies like React and React Native, and specific settings for the Codegen, such as module name, type, and source directories. Ensure placeholders like GitHub handles and author information are updated. ```json { "name": "rtn-calculator", "version": "0.0.1", "description": "Add numbers with Turbo Native Modules", "react-native": "js/index", "source": "js/index", "files": [ "js", "android", "ios", "rtn-calculator.podspec", "!android/build", "!ios/build", "!**/__tests__", "!**/__fixtures__", "!**/__mocks__" ], "keywords": ["react-native", "ios", "android"], "repository": "https://github.com//rtn-calculator", "author": " (https://github.com/)", "license": "MIT", "bugs": { "url": "https://github.com//rtn-calculator/issues" }, "homepage": "https://github.com//rtn-calculator#readme", "devDependencies": {}, "peerDependencies": { "react": "*", "react-native": "*" }, "codegenConfig": { "name": "RTNCalculatorSpec", "type": "modules", "jsSrcsDir": "js", "android": { "javaPackageName": "com.rtncalculator" } } } ``` -------------------------------- ### Install iOS Dependencies for New Architecture (Pod Install) Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/fabric-native-components.md This command installs the necessary iOS dependencies for your React Native project, specifically enabling the New Architecture by setting the `RCT_NEW_ARCH_ENABLED` environment variable. This ensures that Codegen operations are performed correctly for native component integration. ```sh cd ios RCT_NEW_ARCH_ENABLED=1 bundle exec pod install ``` -------------------------------- ### Run Codegen CLI (Android, Custom Paths) Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/codegen.md This example demonstrates running the Codegen CLI for Android, specifying a custom path to package.json and a dedicated output directory for generated files. ```sh npx react-native codegen \ --path third-party/some-library \ --platform android \ --outputPath third-party/some-library/android/generated ``` -------------------------------- ### RTNCalculator Native Header Interface (iOS) Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules.md This Objective-C header file defines the interface for the RTNCalculator native module. It conforms to the NativeRTNCalculatorSpec protocol, which is generated by Codegen. This file is used to declare native methods that can be invoked from JavaScript. For this example, the interface is empty as no specific methods are required. ```objc #import NS_ASSUME_NONNULL_BEGIN @interface RTNCalculator : NSObject @end NS_ASSUME_NONNULL_END ``` -------------------------------- ### Use Turbo Native Module Calculator in React Native App (Flow) Source: https://github.com/reactwg/react-native-new-architecture/blob/main/docs/turbo-modules.md Example of an App.js file using the `add` method from a Turbo Native Module 'RTNCalculator'. This snippet requires the 'rtn-calculator' package and demonstrates basic asynchronous method invocation and state updates in React Native. ```typescript /** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow strict-local */ import React from "react"; import { useState } from "react"; import type { Node } from "react"; import { SafeAreaView, StatusBar, Text, Button } from "react-native"; import RTNCalculator from "rtn-calculator/js/NativeCalculator"; const App: () => Node = () => { const [result, setResult] = useState(null); return ( 3+7={result ?? "??"}