Skip to content

Commit e4c9e93

Browse files
author
Harini Malothu
committed
Update references poitnting to REACT_MODULE
1 parent f0859cc commit e4c9e93

7 files changed

Lines changed: 27 additions & 21 deletions

File tree

docs/native-modules-async.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ Let's pause this and start building our native module:
161161
```cpp
162162
namespace NativeModuleSample
163163
{
164-
REACT_MODULE(SimpleHttpModule);
164+
REACT_TURBO_MODULE(SimpleHttpModule);
165165
struct SimpleHttpModule
166166
{
167167
REACT_METHOD(GetHttpResponse);

docs/native-modules.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,16 @@ The `Microsoft.ReactNative.Managed.ReactPackageProvider` is a convenience that m
235235

236236
| Attribute | Use |
237237
| ------------------------ | --------------------------------------------------------- |
238-
| `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`. |
239240
| `REACT_MODULE_NOREG` | Specifies the class is a native module. |
240241
| `REACT_METHOD` | Specifies an asynchronous method. |
241242
| `REACT_SYNC_METHOD` | Specifies a synchronous method. |
242243
| `REACT_GET_CONSTANTS` | Specifies a method that provides a set of constants. (Preferred) |
243244
| `REACT_CONSTANT` | Specifies a field or property that represents a constant. |
244245
| `REACT_CONSTANTPROVIDER` | Specifies a method that provides a set of constants. |
245246
| `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`). |
247248
| `REACT_INIT` | Specifies a class initialization module. |
248249
| `ReactFunction` | Specifies a JavaScript function that you want exposed to your native code. |
249250

@@ -268,7 +269,7 @@ Here is a sample native module written in C++ called `FancyMath`. It is a simple
268269

269270
namespace NativeModuleSample
270271
{
271-
REACT_MODULE(FancyMath);
272+
REACT_TURBO_MODULE(FancyMath);
272273
struct FancyMath
273274
{
274275
// The namespace here will align with the codegenConfig.windows.namespace property in your package.json
@@ -296,15 +297,17 @@ namespace NativeModuleSample
296297
}
297298
```
298299
299-
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.
300303
301304
It is important to specify the `ModuleSpec`. Defining `ModuleSpec` will enforce that your module defines the same methods as the JS spec file.
302305
303306
> 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.
304307
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")`.
306309
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).
308311
309312
> 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.
310313
@@ -436,7 +439,7 @@ If `REACT_MODULE_NOREG` is used instead of `REACT_MODULE`, `AddAttributedModules
436439
packageBuilder.AddTurboModule(L"FancyMath", MakeModuleProvider<NativeModuleSample::FancyMath>());
437440
```
438441

439-
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.
440443

441444
See [Native Modules vs Turbo Modules](native-modules-vs-turbo-modules.md) for more details on TurboModules.
442445

docs/native-platform-modules.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Conveniently our new project already includes implementations we can look at (fo
160160
namespace winrt::testlib
161161
{
162162
163-
REACT_MODULE(Testlib)
163+
REACT_TURBO_MODULE(Testlib)
164164
struct Testlib
165165
{
166166
using ModuleSpec = testlibCodegen::TestlibSpec;
@@ -203,7 +203,7 @@ double Testlib::multiply(double a, double b) noexcept {
203203
204204
<!--END_DOCUSAURUS_CODE_TABS-->
205205
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`.
207207
208208
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.
209209
@@ -313,7 +313,7 @@ void ReactPackageProvider::CreatePackage(IReactPackageBuilder const &packageBuil
313313
314314
Conveniently our new project already includes the code to include our `Testlib` Turbo Native Module.
315315
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.
317317
318318
### 4. Use the Native Module in your JavaScript
319319

samples/NativeModuleSample/cpp-lib/windows/NativeModuleSample/DataMarshallingExamples.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ std::string to_string(const NativeModuleSampleCodegen::DataMarshallingExamplesSp
3838

3939
#pragma endregion
4040

41-
REACT_MODULE(DataMarshallingExamples);
41+
REACT_TURBO_MODULE(DataMarshallingExamples);
4242
struct DataMarshallingExamples {
4343
using ModuleSpec = NativeModuleSampleCodegen::DataMarshallingExamplesSpec;
4444

samples/NativeModuleSample/cpp-lib/windows/NativeModuleSample/FancyMath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace winrt::NativeModuleSample {
2323

24-
REACT_MODULE(FancyMath);
24+
REACT_TURBO_MODULE(FancyMath);
2525
struct FancyMath {
2626
using ModuleSpec = NativeModuleSampleCodegen::FancyMathSpec;
2727

samples/NativeModuleSample/cpp-lib/windows/NativeModuleSample/SimpleHttpModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
namespace winrt::NativeModuleSample {
2222

23-
REACT_MODULE(SimpleHttpModule);
23+
REACT_TURBO_MODULE(SimpleHttpModule);
2424
struct SimpleHttpModule {
2525
using ModuleSpec = NativeModuleSampleCodegen::SimpleHttpModuleSpec;
2626
using Response = NativeModuleSampleCodegen::SimpleHttpModuleSpec_Response;

website/versioned_docs/version-0.80/native-modules.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,16 @@ The `Microsoft.ReactNative.Managed.ReactPackageProvider` is a convenience that m
236236

237237
| Attribute | Use |
238238
| ------------------------ | --------------------------------------------------------- |
239-
| `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`. |
240241
| `REACT_MODULE_NOREG` | Specifies the class is a native module. |
241242
| `REACT_METHOD` | Specifies an asynchronous method. |
242243
| `REACT_SYNC_METHOD` | Specifies a synchronous method. |
243244
| `REACT_GET_CONSTANTS` | Specifies a method that provides a set of constants. (Preferred) |
244245
| `REACT_CONSTANT` | Specifies a field or property that represents a constant. |
245246
| `REACT_CONSTANTPROVIDER` | Specifies a method that provides a set of constants. |
246247
| `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`). |
248249
| `REACT_INIT` | Specifies a class initialization module. |
249250
| `ReactFunction` | Specifies a JavaScript function that you want exposed to your native code. |
250251

@@ -269,7 +270,7 @@ Here is a sample native module written in C++ called `FancyMath`. It is a simple
269270

270271
namespace NativeModuleSample
271272
{
272-
REACT_MODULE(FancyMath);
273+
REACT_TURBO_MODULE(FancyMath);
273274
struct FancyMath
274275
{
275276
// The namespace here will align with the codegenConfig.windows.namespace property in your package.json
@@ -297,15 +298,17 @@ namespace NativeModuleSample
297298
}
298299
```
299300
300-
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.
301304
302305
It is important to specify the `ModuleSpec`. Defining `ModuleSpec` will enforce that your module defines the same methods as the JS spec file.
303306
304307
> 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.
305308
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")`.
307310
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).
309312
310313
> 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.
311314
@@ -437,7 +440,7 @@ If `REACT_MODULE_NOREG` is used instead of `REACT_MODULE`, `AddAttributedModules
437440
packageBuilder.AddTurboModule(L"FancyMath", MakeModuleProvider<NativeModuleSample::FancyMath>());
438441
```
439442

440-
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.
441444

442445
See [Native Modules vs Turbo Modules](native-modules-vs-turbo-modules.md) for more details on TurboModules.
443446

0 commit comments

Comments
 (0)