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
|`REACT_MODULE`| Specifies the class is a native module, making this module recognizable by `AddAttributedModules` function. |
238
+
|`REACT_TURBO_MODULE`| Specifies the class is a native TurboModule (recommended). Required when using `REACT_EVENT` with `NativeEventEmitter`. |
239
+
|`REACT_MODULE`| Specifies the class is a native module (legacy). Use `REACT_TURBO_MODULE` for new modules. Makes the module recognizable by `AddAttributedModules`. |
239
240
|`REACT_MODULE_NOREG`| Specifies the class is a native module. |
240
241
|`REACT_METHOD`| Specifies an asynchronous method. |
241
242
|`REACT_SYNC_METHOD`| Specifies a synchronous method. |
242
243
|`REACT_GET_CONSTANTS`| Specifies a method that provides a set of constants. (Preferred) |
243
244
|`REACT_CONSTANT`| Specifies a field or property that represents a constant. |
244
245
|`REACT_CONSTANTPROVIDER`| Specifies a method that provides a set of constants. |
245
246
|`REACT_EVENT`| Specifies a field or property that represents an event. |
246
-
|`REACT_STRUCT`| Specifies a `struct` that can be used in native methods (don't nest the definition inside `REACT_MODULE`).|
247
+
|`REACT_STRUCT`| Specifies a `struct` that can be used in native methods (don't nest the definition inside `REACT_TURBO_MODULE` or `REACT_MODULE`). |
247
248
|`REACT_INIT`| Specifies a class initialization module. |
248
249
|`ReactFunction`| Specifies a JavaScript function that you want exposed to your native code. |
249
250
@@ -268,7 +269,7 @@ Here is a sample native module written in C++ called `FancyMath`. It is a simple
268
269
269
270
namespaceNativeModuleSample
270
271
{
271
-
REACT_MODULE(FancyMath);
272
+
REACT_TURBO_MODULE(FancyMath);
272
273
struct FancyMath
273
274
{
274
275
// The namespace here will align with the codegenConfig.windows.namespace property in your package.json
The `REACT_MODULE` macro-attribute says that the class is a React Native native module. It receives the class name as a first parameter. All other macro-attributes also receive their target as a first parameter. `REACT_MODULE` has an optional parameter for the module name visible to JavaScript and optionally the name of a registered event emitter. By default, the name visible to JavaScript is the same as the class name, and the default event emitter is `RCTDeviceEventEmitter`.
300
+
The `REACT_TURBO_MODULE` macro-attribute says that the class is a React Native TurboModule. It receives the class name as a first parameter. All other macro-attributes also receive their target as a first parameter. `REACT_TURBO_MODULE` has an optional parameter for the module name visible to JavaScript. By default, the name visible to JavaScript is the same as the class name.
301
+
302
+
> **NOTE:** Use `REACT_TURBO_MODULE` for all new native modules. It is required when your module uses `REACT_EVENT` and you want to subscribe to events via `NativeEventEmitter` in JavaScript. `REACT_MODULE` is legacy support for older React Native versions that lack native TurboModule event emitters — use it only when targeting older RN versions.
300
303
301
304
It is important to specify the `ModuleSpec`. Defining `ModuleSpec` will enforce that your module defines the same methods as the JS spec file.
302
305
303
306
> NOTE: Methods annotated with `REACT_METHOD` and friends must have the `noexcept` specifier, otherwise the program will not compile. Module authors should make sure all exceptions are handled inside the method.
304
307
305
-
You can overwrite the JavaScript module name like this: `REACT_MODULE(FancyMath, L"math")`.
308
+
You can overwrite the JavaScript module name like this: `REACT_TURBO_MODULE(FancyMath, L"math")`.
306
309
307
-
You can specify a different event emitter like this: `REACT_MODULE(FancyMath, L"math", L"mathEmitter")`.
310
+
> NOTE: `REACT_TURBO_MODULE` does not support a custom event emitter parameter. If you need a custom event emitter, use `REACT_MODULE(FancyMath, L"math", L"mathEmitter")` (legacy).
308
311
309
312
> NOTE: Using the default event emitter, `RCTDeviceEventEmitter`, all native event names must be **globally unique across all native modules** (even the ones built-in to RN). However, specifying your own event emitter means you'll need to create and register that too. This process is outlined in the [Native Modules and React Native Windows (Advanced Topics)](native-modules-advanced.md) document.
310
313
@@ -436,7 +439,7 @@ If `REACT_MODULE_NOREG` is used instead of `REACT_MODULE`, `AddAttributedModules
Here we've implemented the `CreatePackage` method, which receives `packageBuilder` to build contents of the package. Since we use macros and templates to discover and bind native module, we call `AddAttributedModules` function to register all native modules in our DLL that have the `REACT_MODULE` macro-attribute. Specifying true here will register all the native modules as TurboModules rather than Native Modules. This will avoid some additional serialization that happens with Native Module calls. If for some reason you need the modules to continue to run as Native Modules, you can specify false here.
442
+
Here we've implemented the `CreatePackage` method, which receives `packageBuilder` to build contents of the package. Since we use macros and templates to discover and bind native module, we call `AddAttributedModules` function to register all native modules in our DLL that have the `REACT_TURBO_MODULE` or `REACT_MODULE` macro-attribute. Specifying `true` here registers all modules as TurboModules, which avoids additional serialization overhead. If you need modules to run as legacy Native Modules, you can pass `false` — but this is not recommended for new code.
440
443
441
444
See [Native Modules vs Turbo Modules](native-modules-vs-turbo-modules.md) for more details on TurboModules.
Copy file name to clipboardExpand all lines: docs/native-platform-modules.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -160,7 +160,7 @@ Conveniently our new project already includes implementations we can look at (fo
160
160
namespace winrt::testlib
161
161
{
162
162
163
-
REACT_MODULE(Testlib)
163
+
REACT_TURBO_MODULE(Testlib)
164
164
struct Testlib
165
165
{
166
166
using ModuleSpec = testlibCodegen::TestlibSpec;
@@ -203,7 +203,7 @@ double Testlib::multiply(double a, double b) noexcept {
203
203
204
204
<!--END_DOCUSAURUS_CODE_TABS-->
205
205
206
-
As you can see, the `testlib.h` file defines a `Testlib` struct, attributed with `REACT_MODULE` to signify to React Native for Windows that this struct contains the implementation of the Turbo Native Module named `Testlib`.
206
+
As you can see, the `testlib.h` file defines a `Testlib` struct, attributed with `REACT_TURBO_MODULE` to signify to React Native for Windows that this struct contains the implementation of the Turbo Native Module named `Testlib`.
207
207
208
208
The `using ModuleSpec = testlibCodegen::TestlibSpec;` line is what makes sure that the `Testlib` struct will fail to compile if it doesn't meet the required API surface of the module.
Conveniently our new project already includes the code to include our `Testlib` Turbo Native Module.
315
315
316
-
The key bit here is the `#include "testlib.h"` include and the call to the `AddAttributedModules` function. This call makes sure that every Turbo Native Module (i.e. every struct attributed with `REACT_MODULE`), from every included header file, gets included in the library's package.
316
+
The key bit here is the `#include "testlib.h"` include and the call to the `AddAttributedModules` function. This call makes sure that every Turbo Native Module (i.e. every struct attributed with `REACT_TURBO_MODULE`), from every included header file, gets included in the library's package.
|`REACT_MODULE`| Specifies the class is a native module, making this module recognizable by `AddAttributedModules` function. |
239
+
|`REACT_TURBO_MODULE`| Specifies the class is a native TurboModule (recommended). Required when using `REACT_EVENT` with `NativeEventEmitter`. |
240
+
|`REACT_MODULE`| Specifies the class is a native module (legacy). Use `REACT_TURBO_MODULE` for new modules. Makes the module recognizable by `AddAttributedModules`. |
240
241
|`REACT_MODULE_NOREG`| Specifies the class is a native module. |
241
242
|`REACT_METHOD`| Specifies an asynchronous method. |
242
243
|`REACT_SYNC_METHOD`| Specifies a synchronous method. |
243
244
|`REACT_GET_CONSTANTS`| Specifies a method that provides a set of constants. (Preferred) |
244
245
|`REACT_CONSTANT`| Specifies a field or property that represents a constant. |
245
246
|`REACT_CONSTANTPROVIDER`| Specifies a method that provides a set of constants. |
246
247
|`REACT_EVENT`| Specifies a field or property that represents an event. |
247
-
|`REACT_STRUCT`| Specifies a `struct` that can be used in native methods (don't nest the definition inside `REACT_MODULE`).|
248
+
|`REACT_STRUCT`| Specifies a `struct` that can be used in native methods (don't nest the definition inside `REACT_TURBO_MODULE` or `REACT_MODULE`). |
248
249
|`REACT_INIT`| Specifies a class initialization module. |
249
250
|`ReactFunction`| Specifies a JavaScript function that you want exposed to your native code. |
250
251
@@ -269,7 +270,7 @@ Here is a sample native module written in C++ called `FancyMath`. It is a simple
269
270
270
271
namespaceNativeModuleSample
271
272
{
272
-
REACT_MODULE(FancyMath);
273
+
REACT_TURBO_MODULE(FancyMath);
273
274
struct FancyMath
274
275
{
275
276
// The namespace here will align with the codegenConfig.windows.namespace property in your package.json
The `REACT_MODULE` macro-attribute says that the class is a React Native native module. It receives the class name as a first parameter. All other macro-attributes also receive their target as a first parameter. `REACT_MODULE` has an optional parameter for the module name visible to JavaScript and optionally the name of a registered event emitter. By default, the name visible to JavaScript is the same as the class name, and the default event emitter is `RCTDeviceEventEmitter`.
301
+
The `REACT_TURBO_MODULE` macro-attribute says that the class is a React Native TurboModule. It receives the class name as a first parameter. All other macro-attributes also receive their target as a first parameter. `REACT_TURBO_MODULE` has an optional parameter for the module name visible to JavaScript. By default, the name visible to JavaScript is the same as the class name.
302
+
303
+
> **NOTE:** Use `REACT_TURBO_MODULE` for all new native modules. It is required when your module uses `REACT_EVENT` and you want to subscribe to events via `NativeEventEmitter` in JavaScript. `REACT_MODULE` is legacy support for older React Native versions that lack native TurboModule event emitters — use it only when targeting older RN versions.
301
304
302
305
It is important to specify the `ModuleSpec`. Defining `ModuleSpec` will enforce that your module defines the same methods as the JS spec file.
303
306
304
307
> NOTE: Methods annotated with `REACT_METHOD` and friends must have the `noexcept` specifier, otherwise the program will not compile. Module authors should make sure all exceptions are handled inside the method.
305
308
306
-
You can overwrite the JavaScript module name like this: `REACT_MODULE(FancyMath, L"math")`.
309
+
You can overwrite the JavaScript module name like this: `REACT_TURBO_MODULE(FancyMath, L"math")`.
307
310
308
-
You can specify a different event emitter like this: `REACT_MODULE(FancyMath, L"math", L"mathEmitter")`.
311
+
> NOTE: `REACT_TURBO_MODULE` does not support a custom event emitter parameter. If you need a custom event emitter, use `REACT_MODULE(FancyMath, L"math", L"mathEmitter")` (legacy).
309
312
310
313
> NOTE: Using the default event emitter, `RCTDeviceEventEmitter`, all native event names must be **globally unique across all native modules** (even the ones built-in to RN). However, specifying your own event emitter means you'll need to create and register that too. This process is outlined in the [Native Modules and React Native Windows (Advanced Topics)](native-modules-advanced.md) document.
311
314
@@ -437,7 +440,7 @@ If `REACT_MODULE_NOREG` is used instead of `REACT_MODULE`, `AddAttributedModules
Here we've implemented the `CreatePackage` method, which receives `packageBuilder` to build contents of the package. Since we use macros and templates to discover and bind native module, we call `AddAttributedModules` function to register all native modules in our DLL that have the `REACT_MODULE` macro-attribute. Specifying true here will register all the native modules as TurboModules rather than Native Modules. This will avoid some additional serialization that happens with Native Module calls. If for some reason you need the modules to continue to run as Native Modules, you can specify false here.
443
+
Here we've implemented the `CreatePackage` method, which receives `packageBuilder` to build contents of the package. Since we use macros and templates to discover and bind native module, we call `AddAttributedModules` function to register all native modules in our DLL that have the `REACT_TURBO_MODULE` or `REACT_MODULE` macro-attribute. Specifying `true` here registers all modules as TurboModules, which avoids additional serialization overhead. If you need modules to run as legacy Native Modules, you can pass `false` — but this is not recommended for new code.
441
444
442
445
See [Native Modules vs Turbo Modules](native-modules-vs-turbo-modules.md) for more details on TurboModules.
0 commit comments