Merge pull request #2081 from MattFoley/patch-1
Enum Constants Explanation
This commit is contained in:
commit
75b220d6ce
|
@ -230,6 +230,47 @@ console.log(CalendarManager.firstDayOfTheWeek);
|
|||
|
||||
Note that the constants are exported only at initialization time, so if you change `constantsToExport` values at runtime it won't affect the JavaScript environment.
|
||||
|
||||
### Enum Constants
|
||||
|
||||
Enums that are defined via `NS_ENUM` cannot be used as method arguments without first extending RCTConvert.
|
||||
|
||||
In order to export the following `NS_ENUM` definition:
|
||||
|
||||
```objc
|
||||
typedef NS_ENUM(NSInteger, UIStatusBarAnimation) {
|
||||
UIStatusBarAnimationNone,
|
||||
UIStatusBarAnimationFade,
|
||||
UIStatusBarAnimationSlide,
|
||||
};
|
||||
```
|
||||
|
||||
You must create a class extension of RCTConvert like so:
|
||||
|
||||
```objc
|
||||
@implementation RCTConvert (StatusBarAnimation)
|
||||
RCT_ENUM_CONVERTER(UIStatusBarAnimation, (@{ @"statusBarAnimationNone" : @(UIStatusBarAnimationNone),
|
||||
@"statusBarAnimationFade" : @(UIStatusBarAnimationFade),
|
||||
@"statusBarAnimationSlide" : @(UIStatusBarAnimationSlide),
|
||||
UIStatusBarAnimationNone, integerValue)
|
||||
@end
|
||||
```
|
||||
|
||||
You can then define methods and export your enum constants like this:
|
||||
|
||||
```objc
|
||||
- (NSDictionary *)constantsToExport
|
||||
{
|
||||
return @{ @"statusBarAnimationNone" : @(UIStatusBarAnimationNone),
|
||||
@"statusBarAnimationFade" : @(UIStatusBarAnimationFade),
|
||||
@"statusBarAnimationSlide" : @(UIStatusBarAnimationSlide) }
|
||||
};
|
||||
|
||||
RCT_EXPORT_METHOD(updateStatusBarAnimation:(UIStatusBarAnimation)animation
|
||||
completion:(RCTResponseSenderBlock)callback)
|
||||
```
|
||||
|
||||
Your enum will then be automatically unwrapped using the selector provided (`integerValue` in the above example) before being passed to your exported method.
|
||||
|
||||
|
||||
## Sending Events to JavaScript
|
||||
|
||||
|
|
Loading…
Reference in New Issue