Skip to content

Commit c878206

Browse files
docs: Fix typos, code examples, and other inconsistencies in Native Development section (facebook#4290)
1 parent 3736a9c commit c878206

File tree

10 files changed

+107
-127
lines changed

10 files changed

+107
-127
lines changed

docs/appendix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ You may use the following table as a reference for which types are supported and
2929

3030
### Notes:
3131

32-
<b>[1]</b> We strongly recommend using Object literals intead of Objects.
32+
<b>[1]</b> We strongly recommend using Object literals instead of Objects.
3333

3434
:::info
3535
You may also find it useful to refer to the JavaScript specifications for the core modules in React Native. These are located inside the `Libraries/` directory in the React Native repository.

docs/the-new-architecture/codegen-cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The Codegen CLI
22

3-
Calling Gradle or manually calling a script might be hard to remember and it requires a lot of cerimony.
3+
Calling Gradle or manually calling a script might be hard to remember and it requires a lot of ceremony.
44

55
To simplify it, we created a CLI tool that can help you running those tasks: the **Codegen** cli. This command runs [react-native-codegen](https://www.npmjs.com/package/react-native-codegen) for your project. The following options are available:
66

docs/the-new-architecture/create-module-library.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ npx create-react-native-library@latest <Name of Your Library>
3333
4. Continue filling the form until you reach the question _"What type of library do you want to develop?"_
3434
![What type of Library](/docs/assets/what-library.png)
3535
5. For the sake of this guide, select the _Turbo module_ option. Notice that you can create libraries for both New Architecture and Legacy Architecture.
36-
6. Then, you can choose whether you want a library that access the platform (Kotlin & Objective-C) or a sahred C++ library (C++ for Android and iOS).
36+
6. Then, you can choose whether you want a library that access the platform (Kotlin & Objective-C) or a shared C++ library (C++ for Android and iOS).
3737
7. Finally, select the `Test App` as last option. This option creates the library with a separate app already configured within the library folder.
3838

3939
Once the interactive prompt is done, the tool creates a folder whose structure looks like this in Visual Studio Code:
@@ -45,9 +45,9 @@ Feel free to explore the code that has been created for you. However, the most i
4545
- The `android` folder: this is where the Android code lives
4646
- The `cpp` folder: this is where we the c++ code lives
4747
- The `ios` folder: this is where we the iOS code lives
48-
- The `src` forder: this is where the JS code lives.
48+
- The `src` folder: this is where the JS code lives.
4949

50-
The `package.json` is already configured with all the information that we provided to the `create-react-native-library` tool, including the name and the description of the package. Notice that the `package.json` is also already configured to run codegen.
50+
The `package.json` is already configured with all the information that we provided to the `create-react-native-library` tool, including the name and the description of the package. Notice that the `package.json` is also already configured to run Codegen.
5151

5252
```json
5353
"codegenConfig": {
@@ -64,7 +64,7 @@ The `package.json` is already configured with all the information that we provid
6464
},
6565
```
6666

67-
Finally the library contains already all the infrastruction to let the library be linked with iOS and Android.
67+
Finally, the library contains already all the infrastructure to let the library be linked with iOS and Android.
6868

6969
### 2. Copy the Code over from Your App
7070

@@ -105,7 +105,7 @@ To test your library:
105105
4. Build and run Android with `yarn android` from the `example` folder.
106106
5. Build and run iOS with `yarn ios` from the `example` folder.
107107

108-
## Use your library as a Local Module.
108+
## Use your library as a Local Module
109109

110110
There are some scenario where you might want to reuse your library as a local module for your applications, without publishing it to NPM.
111111

@@ -164,7 +164,7 @@ At this point, you can build and run your app as usual:
164164
- Build and run Android with `yarn android` from the `example` folder.
165165
- Build and run iOS with `yarn ios` from the `example` folder.
166166

167-
## Publish the Library on NPM.
167+
## Publish the Library on NPM
168168

169169
The setup to publish everything on NPM is already in place, thanks to `create-react-native-library`.
170170

docs/the-new-architecture/custom-cxx-types.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export default (TurboModuleRegistry.getEnforcing<Spec>(
110110

111111
In this files, we are defining the function that needs to be implemented in C++.
112112

113-
### 3. Implement the Native Code.
113+
### 3. Implement the Native Code
114114

115115
Now, we need to implement the function that we declared in the JS specification.
116116

@@ -161,7 +161,7 @@ std::string NativeSampleModule::reverseString(jsi::Runtime& rt, std::string inpu
161161
} // namespace facebook::react
162162
```
163163

164-
The implementation imports the `<cmath>` C++ library to perform mathematical operations, then it implements the `cubicRoot` function usinf the `cbrt` primitive from the `<cmath>` module.
164+
The implementation imports the `<cmath>` C++ library to perform mathematical operations, then it implements the `cubicRoot` function using the `cbrt` primitive from the `<cmath>` module.
165165

166166
### 4. Test your code in Your App
167167

@@ -211,7 +211,7 @@ First, we need to update the `App.tsx` file to use the new method from the Turbo
211211

212212
## Adding a New Structured Custom Type: Address
213213

214-
The approach above can be generalized to any kind of type. For structured types, React Native provides some helper functions that make it easier to bridge them from JS to C++ and viceversa.
214+
The approach above can be generalized to any kind of type. For structured types, React Native provides some helper functions that make it easier to bridge them from JS to C++ and vice versa.
215215

216216
Let's assume that we want to bridge a custom `Address` type with the following properties:
217217

@@ -288,7 +288,7 @@ It is also possible to have functions that return custom types.
288288

289289
From the `Address` type defined in the specs, Codegen will generate two helper types: `NativeSampleModuleAddress` and `NativeSampleModuleAddressBridging`.
290290

291-
The first type is the definition of the `Address`. The second type contains all the infrastructure to bridge the custom type from JS to C++ and viceversa. The only extra step we need to add is to define the `Bridging` structure that extends the `NativeSampleModuleAddressBridging` type.
291+
The first type is the definition of the `Address`. The second type contains all the infrastructure to bridge the custom type from JS to C++ and vice versa. The only extra step we need to add is to define the `Bridging` structure that extends the `NativeSampleModuleAddressBridging` type.
292292

293293
1. Open the `shared/NativeSampleModule.h` file
294294
2. Add the following code in the file:
@@ -358,7 +358,7 @@ Once we manually parsed the object, we can implement the logic that we need.
358358
If you want to learn more about `JSI` and how it works, have a look at this [great talk](https://youtu.be/oLmGInjKU2U?feature=shared) from App.JS 2024
359359
:::
360360

361-
### 4. Testing the code in the app.
361+
### 4. Testing the code in the app
362362

363363
To test the code in the app, we have to modify the `App.tsx` file.
364364

docs/the-new-architecture/pure-cxx-modules.md

Lines changed: 60 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ npx @react-native-community/cli@latest init SampleApp --version 0.76.0
2222

2323
Pure C++ Turbo Native Modules are Turbo Native Modules. They needs a specification file (also called spec file) so that Codegen can create the scaffolding code for us. The specification file is also what we use to access the Turbo Native Module in JS.
2424

25-
Specs files need to be written in a typed JS dialect. React Native currently supports Flow or TypeScript
25+
Specs files need to be written in a typed JS dialect. React Native currently supports Flow or TypeScript.
2626

2727
1. Inside the root folder of your app, create a new folder called `specs`.
28-
2. Create a new file called `NativeSampleModule.ts`
28+
2. Create a new file called `NativeSampleModule.ts` with the following code:
2929

3030
:::warning
3131
All Native Turbo Module spec files must have the prefix `Native`, otherwise Codegen will ignore them.
@@ -34,7 +34,7 @@ All Native Turbo Module spec files must have the prefix `Native`, otherwise Code
3434
<Tabs groupId="tnm-specs" queryString defaultValue={constants.defaultJavaScriptSpecLanguages} values={constants.javaScriptSpecLanguages}>
3535
<TabItem value="flow">
3636

37-
```ts
37+
```ts title="specs/NativeSampleModule.ts"
3838
// @flow
3939
import type {TurboModule} from 'react-native'
4040
import { TurboModuleRegistry } from "react-native";
@@ -51,7 +51,7 @@ export default (TurboModuleRegistry.getEnforcing<Spec>(
5151
</TabItem>
5252
<TabItem value="typescript">
5353

54-
```ts
54+
```ts title="specs/NativeSampleModule.ts"
5555
import {TurboModule, TurboModuleRegistry} from 'react-native';
5656

5757
export interface Spec extends TurboModule {
@@ -68,60 +68,23 @@ export default TurboModuleRegistry.getEnforcing<Spec>(
6868

6969
## 2. Configure Codegen
7070

71-
The next step is to configure [Codegen](what-is-codegen.md) in your `package.json`.
72-
73-
1. Open the `package.json` file
74-
2. Modify it as it follows:
75-
76-
```diff
77-
{
78-
"name": "SampleApp",
79-
"version": "0.0.1",
80-
"private": true,
81-
"scripts": {
82-
"android": "react-native run-android",
83-
"ios": "react-native run-ios",
84-
"lint": "eslint .",
85-
"start": "react-native start",
86-
"test": "jest"
87-
},
88-
"dependencies": {
89-
"react": "18.3.1",
90-
"react-native": "0.76.0"
91-
},
92-
"devDependencies": {
93-
"@babel/core": "^7.25.2",
94-
"@babel/preset-env": "^7.25.3",
95-
"@babel/runtime": "^7.25.0",
96-
"@react-native-community/cli": "15.0.0",
97-
"@react-native-community/cli-platform-android": "15.0.0",
98-
"@react-native-community/cli-platform-ios": "15.0.0",
99-
"@react-native/babel-preset": "0.76.0",
100-
"@react-native/eslint-config": "0.76.0",
101-
"@react-native/metro-config": "0.76.0",
102-
"@react-native/typescript-config": "0.76.0",
103-
"@types/react": "^18.2.6",
104-
"@types/react-test-renderer": "^18.0.0",
105-
"babel-jest": "^29.6.3",
106-
"eslint": "^8.19.0",
107-
"jest": "^29.6.3",
108-
"prettier": "2.8.8",
109-
"react-test-renderer": "18.3.1",
110-
"typescript": "5.0.4"
111-
},
112-
"engines": {
113-
"node": ">=18"
114-
},
115-
+ "codegenConfig": {
116-
+ "name": "AppSpecs",
117-
+ "type": "modules",
118-
+ "jsSrcsDir": "specs",
119-
+ "android": {
120-
+ "javaPackageName": "com.sampleapp.specs"
121-
+ }
122-
+ },
123-
"packageManager": "[email protected]"
124-
}
71+
The next step is to configure [Codegen](what-is-codegen.md) in your `package.json`. Update the file to include:
72+
73+
```json title="package.json"
74+
"start": "react-native start",
75+
"test": "jest"
76+
},
77+
// highlight-add-start
78+
"codegenConfig": {
79+
"name": "AppSpecs",
80+
"type": "modules",
81+
"jsSrcsDir": "specs",
82+
"android": {
83+
"javaPackageName": "com.sampleapp.specs"
84+
}
85+
},
86+
// highlight-add-end
87+
"dependencies": {
12588
```
12689

12790
This configuration tells Codegen to look for specs files in the `specs` folder. It also instruct Codegen to only generate code for `modules` and to namespace the generated code as `AppSpecs`.
@@ -133,7 +96,7 @@ Writing a C++ Turbo Native Module allow you to share the code between Android an
13396
1. Create a folder named `shared` at the same level of the `android` and `ios` folders.
13497
2. Inside the `shared` folder, create a new file called `NativeSampleModule.h`.
13598

136-
```cpp
99+
```cpp title="shared/NativeSampleModule.h"
137100
#pragma once
138101

139102
#include <AppSpecsJSI.h>
@@ -156,7 +119,7 @@ Writing a C++ Turbo Native Module allow you to share the code between Android an
156119

157120
3. Inside the `shared` folder, create a new file called `NativeSampleModule.cpp`.
158121

159-
```cpp
122+
```cpp title="shared/NativeSampleModule.cpp"
160123
#include "NativeSampleModule.h"
161124

162125
namespace facebook::react {
@@ -171,15 +134,12 @@ Writing a C++ Turbo Native Module allow you to share the code between Android an
171134
} // namespace facebook::react
172135
```
173136
174-
Let's have a look at the two files we created.
175-
176-
The `NativeSampleModule.h` file is the header file for a Pure C++ TurboModule. The `include` statements make sure that we include the specs that will be created by Codegen and that contains the interface and the base class we need to implement.
177-
178-
The module lives in the `facebook::react` namespace to have access to all the types that live in that namespace.
137+
Let's have a look at the two files we created:
179138
180-
The class `NativeSampleModule` is the actual Turbo Native Mdoule class and it extends the `NativeSampleModuleCxxSpec` class which contains some glue code and boilerplate code to let this class behave as a Turbo Native Module.
181-
182-
Finally, we have the constructor, that accepts a pointer to the `CallInvoker`, to communicate with JS if needed and the function's prototype we have to implement.
139+
- The `NativeSampleModule.h` file is the header file for a Pure C++ TurboModule. The `include` statements make sure that we include the specs that will be created by Codegen and that contains the interface and the base class we need to implement.
140+
- The module lives in the `facebook::react` namespace to have access to all the types that live in that namespace.
141+
- The class `NativeSampleModule` is the actual Turbo Native Module class and it extends the `NativeSampleModuleCxxSpec` class which contains some glue code and boilerplate code to let this class behave as a Turbo Native Module.
142+
- Finally, we have the constructor, that accepts a pointer to the `CallInvoker`, to communicate with JS if needed and the function's prototype we have to implement.
183143
184144
The `NativeSampleModule.cpp` files is the actual implementation of our Turbo Native Module and implements the constructor and the method that we declared in the specs.
185145
@@ -193,18 +153,18 @@ This is the only time when we will have to write some platform-specific code.
193153
194154
To make sure that the Android app can effectively build the C++ Turbo Native Module, we need to:
195155
196-
1. Create a `CMakeLists.txt` to access our C++ code
156+
1. Create a `CMakeLists.txt` to access our C++ code.
197157
2. Modify `build.gradle` to point to the newly created `CMakeLists.txt` file.
198158
3. Create an `OnLoad.cpp` file in our Android app to register the new Turbo Native Module.
199159
200160
#### 1. Create the `CMakeLists.txt` file
201161
202162
Android uses CMake to build. CMake needs to access the files we defined in our shared folder, to be able to build them.
203163
204-
1. Create a new folder `SampleApp/android/app/src/main/jni`. The `jni` folder is where the C++ side of Android lives
205-
2. Create a `CMakeLists.txt` file and add this context
164+
1. Create a new folder `SampleApp/android/app/src/main/jni`. The `jni` folder is where the C++ side of Android lives.
165+
2. Create a `CMakeLists.txt` file and add this context:
206166
207-
```shell
167+
```shell title="CMakeLists.txt"
208168
cmake_minimum_required(VERSION 3.13)
209169
210170
# Define the library name here.
@@ -223,18 +183,18 @@ target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ../../../../../shared)
223183
The CMake file does the following things:
224184

225185
- Defines the `appmodules` library, where all the app C++ code will be included.
226-
- Loads the base React Native's CMake file
227-
- Adds the Module C++ source code that we need to build with the `target_sources` directives. By default React Native will already populate the `appmodules` library with default sources, here we include our custom one. You can see that we need to crawl back from the `jni` folder to the `shared` folder where our C++ TM lives.
228-
- Specify where CMake can find the module header files. Also in this case we need to crawl back from the `jni` folder.
186+
- Loads the base React Native's CMake file.
187+
- Adds the Module C++ source code that we need to build with the `target_sources` directives. By default React Native will already populate the `appmodules` library with default sources, here we include our custom one. You can see that we need to crawl back from the `jni` folder to the `shared` folder where our C++ Turbo Module lives.
188+
- Specifies where CMake can find the module header files. Also in this case we need to crawl back from the `jni` folder.
229189

230190
#### 2. Modify `build.gradle` to include the custom C++ code
231191

232192
Gradle is the tool that orchestrates the Android build. We need to tell it where it can find the `CMake` files to build the Turbo Native Module.
233193

234-
1. Open the `SampleApp/android/app/build.gradle` file
194+
1. Open the `SampleApp/android/app/build.gradle` file.
235195
2. Add the following block into the Gradle file, within the existent `android` block:
236196

237-
```diff
197+
```diff title="android/app/build.gradle"
238198
buildTypes {
239199
debug {
240200
signingConfig signingConfigs.debug
@@ -270,7 +230,7 @@ curl -O https://raw.githubusercontent.com/facebook/react-native/v0.76.0/packages
270230

271231
2. Then, modify this file as it follows:
272232

273-
```diff
233+
```diff title="android/app/src/main/jni/OnLoad.cpp"
274234
#include <DefaultComponentsRegistry.h>
275235
#include <DefaultTurboModuleManagerDelegate.h>
276236
#include <autolinking.h>
@@ -325,39 +285,44 @@ To make sure that the iOS app can effectively build the C++ Turbo Native Module,
325285

326286
#### 1. Install Pods and Run Codegen.
327287

328-
The first step we need to run is the usual steps we run every time we have to prepare our iOS application. CocoaPods is the tool we use to setup React Native dependencies and, as part of the process, it will also run Codegen for us.
288+
The first step we need to run is the usual steps we run every time we have to prepare our iOS application. CocoaPods is the tool we use to setup and install React Native dependencies and, as part of the process, it will also run Codegen for us.
329289

330-
1. Navigate to the `ios` folder
331-
2. Run `bundle install` to install the Ruby bundler
332-
3. Run `bundle exec pod install` to install the dependencies and run Codegen
290+
```bash
291+
cd ios
292+
bundle install
293+
bundle exec pod install
294+
```
333295

334296
#### 2. Add the shared folder to the iOS project
335297

336298
This steps adds the `shared` folder to the project to make it visible to xcode.
337299

338-
1. Open the `SampleApp.xcworkspace` file in Xcode.
339-
2. Click on the `SampleApp` project on the left.
340-
3. Select `Add files to "Sample App"...`
341-
4. Select the `shared` folder and click on `Add`
300+
1. Open the CocoPods generated Xcode Workspace.
342301

343-
These images shows you how to add the folder to the project:
302+
```bash
303+
cd ios
304+
open SampleApp.xcworkspace
305+
```
306+
307+
2. Click on the `SampleApp` project on the left and select `Add files to "Sample App"...`.
344308

345309
![Add Files to Sample App...](/docs/assets/AddFilesToXcode1.png)
346310

311+
3. Select the `shared` folder and click on `Add`.
312+
347313
![Add Files to Sample App...](/docs/assets/AddFilesToXcode2.png)
348314

349315
If you did everything right, your project on the left should look like this:
350316

351317
![Xcode Project](/docs/assets/CxxTMGuideXcodeProject.png)
352318

353-
#### 3. Registering the Cxx Turbo Native Module in your app.
319+
#### 3. Registering the Cxx Turbo Native Module in your app
354320

355321
With this last step, we will tell the iOS app where to look for to find the pure C++ Turbo Native Module.
356322

357-
1. In Xcode, open the `AppDelegate.mm` file
358-
2. Modify it as it follows:
323+
In Xcode, open the `AppDelegate.mm` file and modify it as follows:
359324

360-
```diff
325+
```diff title="SampleApp/AppDelegate.mm"
361326
#import <React/RCTBundleURLProvider.h>
362327
+ #import <RCTAppDelegate+Protected.h>
363328
+ #import "NativeSampleModule.h"
@@ -392,10 +357,10 @@ If you now build your application from Xcode, you should be able to build succes
392357

393358
It's now time to access our C++ Turbo Native Module from JS. To do so, we have to modify the `App.tsx` file to import the Turbo Native Module and to call it in our code.
394359

395-
1. Open the `App.tsx` file
396-
2. Replace the content of the template with the following code
360+
1. Open the `App.tsx` file.
361+
2. Replace the content of the template with the following code:
397362

398-
```ts
363+
```tsx title="App.tsx"
399364
import React from 'react';
400365
import {
401366
Button,
@@ -460,7 +425,7 @@ export default App;
460425

461426
The interesting lines in this app are:
462427

463-
- `import SampleTurboModule from './specs/NativeSampleModule';`: this lines imports the Turbo Native Module in the app
428+
- `import SampleTurboModule from './specs/NativeSampleModule';`: this line imports the Turbo Native Module in the app,
464429
- `const revString = SampleTurboModule.reverseString(value);` in the `onPress` callback: this is how you can use the Turbo Native Module in your app.
465430

466431
:::warning

0 commit comments

Comments
 (0)