diff --git a/docs/nativemodulesios.html b/docs/nativemodulesios.html index c371646f6d4..fd6c312ebaf 100644 --- a/docs/nativemodulesios.html +++ b/docs/nativemodulesios.html @@ -14,7 +14,8 @@ } @end

Now from your JavaScript file you can call the method like this:

var CalendarManager = require('NativeModules').CalendarManager; -CalendarManager.addEvent('Birthday Party', '4 Privet Drive, Surrey');

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).

Argument types #

React Native supports several types of arguments that can be passed from JavaScript code to native module:

In our CalendarManager example, if we want to pass event date to native, we have to convert it to a string or a number:

RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location date:(NSInteger)secondsSinceUnixEpoch) +CalendarManager.addEvent('Birthday Party', '4 Privet Drive, Surrey');

NOTE: JavaScript method names +The name of the method exported to JavaScript is the native method's name up to the first colon. React Native also defines a macro called RCT_REMAP_METHOD to specify the JavaScript method's name. This is useful when multiple native methods are the same up to the first colon and would have conflicting JavaScript names.

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).

Argument types #

React Native supports several types of arguments that can be passed from JavaScript code to native module:

In our CalendarManager example, if we want to pass event date to native, we have to convert it to a string or a number:

RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location date:(NSInteger)secondsSinceUnixEpoch) { NSDate *date = [NSDate dateWithTimeIntervalSince1970:secondsSinceUnixEpoch]; }

As CalendarManager.addEvent method gets more and more complex, the number of arguments will grow. Some of them might be optional. In this case it's worth considering changing the API a little bit to accept a dictionary of event attributes, like this:

#import "RCTConvert.h" @@ -27,7 +28,7 @@ CalendarManager.: '4 Privet Drive, Surrey', time: date.toTime(), description: '...' -})

NOTE: About array and map

React Native doesn't provide any guarantees about the types of values in these structures. Your native module might expect an array of strings, but if JavaScript calls your method with an array containing numbers and strings, you'll get NSArray with NSNumber and NSString. It is the developer's responsibility to check array/map value types (see RCTConvert for helper methods).

Callbacks #

WARNING

This section is even more experimental than others, we don't have a set of best practices around callbacks yet.

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.

RCT_EXPORT_METHOD(findEvents:(RCTResponseSenderBlock)callback) +})

NOTE: About array and map

React Native doesn't provide any guarantees about the types of values in these structures. Your native module might expect an array of strings, but if JavaScript calls your method with an array containing numbers and strings, you'll get NSArray with NSNumber and NSString. It is the developer's responsibility to check array/map value types (see RCTConvert for helper methods).

Callbacks #

WARNING

This section is even more experimental than others, we don't have a set of best practices around callbacks yet.

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.

RCT_EXPORT_METHOD(findEvents:(RCTResponseSenderBlock)callback) { NSArray *events = ... callback(@[[NSNull null], events]);