Skip to content

Commit 1276499

Browse files
author
Brent Vatne
committed
Replace RCT_EXPORT with RCT_EXPORT_METHOD
1 parent 7047d6e commit 1276499

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ var GeoInfo = React.createClass({
119119

120120
## Extensibility
121121

122-
It is certainly possible to create a great app using React Native without writing a single line of native code, but React Native is also designed to be easily extended with custom native views and modules - that means you can reuse anything you've already built, and can import and use your favorite native libraries. To create a simple module in iOS, create a new class that implements the `RCTBridgeModule` protocol, and add `RCT_EXPORT` to the function you want to make available in JavaScript.
122+
It is certainly possible to create a great app using React Native without writing a single line of native code, but React Native is also designed to be easily extended with custom native views and modules - that means you can reuse anything you've already built, and can import and use your favorite native libraries. To create a simple module in iOS, create a new class that implements the `RCTBridgeModule` protocol, and wrap the function that you want to make available to JavaScript in `RCT_EXPORT_METHOD`. Additionally, the class itself must be explicitly exported with `RCT_EXPORT_MODULE();`.
123123

124124
```objc
125125
// Objective-C
@@ -130,9 +130,12 @@ It is certainly possible to create a great app using React Native without writin
130130
@end
131131

132132
@implementation MyCustomModule
133-
- (void)processString:(NSString *)input callback:(RCTResponseSenderBlock)callback
133+
134+
RCT_EXPORT_MODULE();
135+
136+
// Available as NativeModules.MyCustomModule.processString
137+
RCT_EXPORT_METHOD(processString:(NSString *)input callback:(RCTResponseSenderBlock)callback)
134138
{
135-
RCT_EXPORT(); // available as NativeModules.MyCustomModule.processString
136139
callback(@[[input stringByReplacingOccurrencesOfString:@"Goodbye" withString:@"Hello"]]);
137140
}
138141
@end

docs/NativeModulesIOS.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ Native module is just an Objectve-C class that implements `RCTBridgeModule` prot
2828
@end
2929
```
3030

31-
React Native will not expose any methods of `CalendarManager` to JavaScript unless explicitly asked. Fortunately this is pretty easy with `RCT_EXPORT`:
31+
React Native will not expose any methods of `CalendarManager` to JavaScript unless explicitly asked. Fortunately this is pretty easy with `RCT_EXPORT_METHOD`:
3232

3333
```objective-c
3434
// CalendarManager.m
3535
@implementation CalendarManager
3636

37-
- (void)addEventWithName:(NSString *)name location:(NSString *)location
37+
RCT_EXPORT_MODULE();
38+
39+
RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
3840
{
39-
RCT_EXPORT();
4041
RCTLogInfo(@"Pretending to create an event %@ at %@", name, location);
4142
}
4243

@@ -47,12 +48,10 @@ Now from your JavaScript file you can call the method like this:
4748

4849
```javascript
4950
var CalendarManager = require('NativeModules').CalendarManager;
50-
CalendarManager.addEventWithName('Birthday Party', '4 Privet Drive, Surrey');
51+
CalendarManager.addEvent('Birthday Party', '4 Privet Drive, Surrey');
5152
```
5253

53-
Notice that the exported method name was generated from first part of Objective-C selector. Sometimes it results in a non-idiomatic JavaScript name (like the one in our example). You can change the name by supplying an optional argument to `RCT_EXPORT`, e.g. `RCT_EXPORT(addEvent)`.
54-
55-
The return type of the method should always be `void`. React Native bridge is asynchronous, so the only way to pass a result to JavaScript is by using callbacks or emitting events (see below).
54+
The return type of bridge methods is always `void`. React Native bridge is asynchronous, so the only way to pass a result to JavaScript is by using callbacks or emitting events (see below).
5655

5756
## Argument types
5857

@@ -68,9 +67,8 @@ React Native supports several types of arguments that can be passed from JavaScr
6867
In our `CalendarManager` example, if we want to pass event date to native, we have to convert it to a string or a number:
6968

7069
```objective-c
71-
- (void)addEventWithName:(NSString *)name location:(NSString *)location date:(NSInteger)secondsSinceUnixEpoch
70+
RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location date:(NSInteger)secondsSinceUnixEpoch)
7271
{
73-
RCT_EXPORT(addEvent);
7472
NSDate *date = [NSDate dateWithTimeIntervalSince1970:secondsSinceUnixEpoch];
7573
}
7674
```
@@ -80,9 +78,8 @@ As `CalendarManager.addEvent` method gets more and more complex, the number of a
8078
```objective-c
8179
#import "RCTConvert.h"
8280
83-
- (void)addEventWithName:(NSString *)name details:(NSDictionary *)details
81+
RCT_EXPORT_METHOD(addEvent:(NSString *)name details:(NSDictionary *)details)
8482
{
85-
RCT_EXPORT(addEvent);
8683
NSString *location = [RCTConvert NSString:details[@"location"]]; // ensure location is a string
8784
...
8885
}
@@ -112,9 +109,8 @@ CalendarManager.addEvent('Birthday Party', {
112109
Native module also supports a special kind of argument- a callback. In most cases it is used to provide the function call result to JavaScript.
113110

114111
```objective-c
115-
- (void)findEvents:(RCTResponseSenderBlock)callback
112+
RCT_EXPORT_METHOD(findEvents:(RCTResponseSenderBlock)callback)
116113
{
117-
RCT_EXPORT();
118114
NSArray *events = ...
119115
callback(@[[NSNull null], events]);
120116
}
@@ -142,9 +138,8 @@ The native module should not have any assumptions about what thread it is being
142138

143139

144140
```objective-c
145-
- (void)addEventWithName:(NSString *)name callback:(RCTResponseSenderBlock)callback
141+
RCT_EXPORT_METHOD(addEvent:(NSString *)name callback:(RCTResponseSenderBlock)callback)
146142
{
147-
RCT_EXPORT(addEvent);
148143
dispatch_async(dispatch_get_main_queue(), ^{
149144
// Call iOS API on main thread
150145
...

0 commit comments

Comments
 (0)