You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/appendix.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ You may use the following table as a reference for which types are supported and
29
29
30
30
### Notes:
31
31
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.
33
33
34
34
:::info
35
35
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.
Copy file name to clipboardExpand all lines: docs/the-new-architecture/codegen-cli.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# The Codegen CLI
2
2
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.
4
4
5
5
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:
Copy file name to clipboardExpand all lines: docs/the-new-architecture/create-module-library.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ npx create-react-native-library@latest <Name of Your Library>
33
33
4. Continue filling the form until you reach the question _"What type of library do you want to develop?"_
34
34

35
35
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).
37
37
7. Finally, select the `Test App` as last option. This option creates the library with a separate app already configured within the library folder.
38
38
39
39
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
45
45
- The `android` folder: this is where the Android code lives
46
46
- The `cpp` folder: this is where we the c++ code lives
47
47
- 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.
49
49
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.
51
51
52
52
```json
53
53
"codegenConfig": {
@@ -64,7 +64,7 @@ The `package.json` is already configured with all the information that we provid
64
64
},
65
65
```
66
66
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.
68
68
69
69
### 2. Copy the Code over from Your App
70
70
@@ -105,7 +105,7 @@ To test your library:
105
105
4. Build and run Android with `yarn android` from the `example` folder.
106
106
5. Build and run iOS with `yarn ios` from the `example` folder.
107
107
108
-
## Use your library as a Local Module.
108
+
## Use your library as a Local Module
109
109
110
110
There are some scenario where you might want to reuse your library as a local module for your applications, without publishing it to NPM.
111
111
@@ -164,7 +164,7 @@ At this point, you can build and run your app as usual:
164
164
- Build and run Android with `yarn android` from the `example` folder.
165
165
- Build and run iOS with `yarn ios` from the `example` folder.
166
166
167
-
## Publish the Library on NPM.
167
+
## Publish the Library on NPM
168
168
169
169
The setup to publish everything on NPM is already in place, thanks to `create-react-native-library`.
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.
165
165
166
166
### 4. Test your code in Your App
167
167
@@ -211,7 +211,7 @@ First, we need to update the `App.tsx` file to use the new method from the Turbo
211
211
212
212
## Adding a New Structured Custom Type: Address
213
213
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.
215
215
216
216
Let's assume that we want to bridge a custom `Address` type with the following properties:
217
217
@@ -288,7 +288,7 @@ It is also possible to have functions that return custom types.
288
288
289
289
From the `Address` type defined in the specs, Codegen will generate two helper types: `NativeSampleModuleAddress` and `NativeSampleModuleAddressBridging`.
290
290
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.
292
292
293
293
1. Open the `shared/NativeSampleModule.h` file
294
294
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.
358
358
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
359
359
:::
360
360
361
-
### 4. Testing the code in the app.
361
+
### 4. Testing the code in the app
362
362
363
363
To test the code in the app, we have to modify the `App.tsx` file.
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.
24
24
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.
26
26
27
27
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:
29
29
30
30
:::warning
31
31
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
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": {
125
88
```
126
89
127
90
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
133
96
1. Create a folder named `shared` at the same level of the `android` and `ios` folders.
134
97
2. Inside the `shared` folder, create a new file called `NativeSampleModule.h`.
135
98
136
-
```cpp
99
+
```cpp title="shared/NativeSampleModule.h"
137
100
#pragma once
138
101
139
102
#include <AppSpecsJSI.h>
@@ -156,7 +119,7 @@ Writing a C++ Turbo Native Module allow you to share the code between Android an
156
119
157
120
3. Inside the `shared` folder, create a new file called `NativeSampleModule.cpp`.
158
121
159
-
```cpp
122
+
```cpp title="shared/NativeSampleModule.cpp"
160
123
#include"NativeSampleModule.h"
161
124
162
125
namespacefacebook::react {
@@ -171,15 +134,12 @@ Writing a C++ Turbo Native Module allow you to share the code between Android an
171
134
} // namespace facebook::react
172
135
```
173
136
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:
179
138
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.
183
143
184
144
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.
185
145
@@ -193,18 +153,18 @@ This is the only time when we will have to write some platform-specific code.
193
153
194
154
To make sure that the Android app can effectively build the C++ Turbo Native Module, we need to:
195
155
196
-
1. Create a `CMakeLists.txt` to access our C++ code
156
+
1. Create a `CMakeLists.txt` to access our C++ code.
197
157
2. Modify `build.gradle` to point to the newly created `CMakeLists.txt` file.
198
158
3. Create an `OnLoad.cpp` file in our Android app to register the new Turbo Native Module.
199
159
200
160
#### 1. Create the `CMakeLists.txt` file
201
161
202
162
Android uses CMake to build. CMake needs to access the files we defined in our shared folder, to be able to build them.
203
163
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:
206
166
207
-
```shell
167
+
```shell title="CMakeLists.txt"
208
168
cmake_minimum_required(VERSION 3.13)
209
169
210
170
# Define the library name here.
@@ -223,18 +183,18 @@ target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ../../../../../shared)
223
183
The CMake file does the following things:
224
184
225
185
- 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.
229
189
230
190
#### 2. Modify `build.gradle` to include the custom C++ code
231
191
232
192
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.
233
193
234
-
1. Open the `SampleApp/android/app/build.gradle` file
194
+
1. Open the `SampleApp/android/app/build.gradle` file.
235
195
2. Add the following block into the Gradle file, within the existent `android` block:
@@ -325,39 +285,44 @@ To make sure that the iOS app can effectively build the C++ Turbo Native Module,
325
285
326
286
#### 1. Install Pods and Run Codegen.
327
287
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.
329
289
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
+
```
333
295
334
296
#### 2. Add the shared folder to the iOS project
335
297
336
298
This steps adds the `shared` folder to the project to make it visible to xcode.
337
299
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.
342
301
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"...`.
344
308
345
309

346
310
311
+
3. Select the `shared` folder and click on `Add`.
312
+
347
313

348
314
349
315
If you did everything right, your project on the left should look like this:
#### 3. Registering the Cxx Turbo Native Module in your app.
319
+
#### 3. Registering the Cxx Turbo Native Module in your app
354
320
355
321
With this last step, we will tell the iOS app where to look for to find the pure C++ Turbo Native Module.
356
322
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:
359
324
360
-
```diff
325
+
```diff title="SampleApp/AppDelegate.mm"
361
326
#import <React/RCTBundleURLProvider.h>
362
327
+ #import <RCTAppDelegate+Protected.h>
363
328
+ #import "NativeSampleModule.h"
@@ -392,10 +357,10 @@ If you now build your application from Xcode, you should be able to build succes
392
357
393
358
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.
394
359
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:
397
362
398
-
```ts
363
+
```tsx title="App.tsx"
399
364
importReactfrom'react';
400
365
import {
401
366
Button,
@@ -460,7 +425,7 @@ export default App;
460
425
461
426
The interesting lines in this app are:
462
427
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,
464
429
-`const revString = SampleTurboModule.reverseString(value);` in the `onPress` callback: this is how you can use the Turbo Native Module in your app.
0 commit comments