Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] NS_ENUM exported to JS, used as method arguments for native modules, must use RCT_ENUM_CONVERTER in RCTConvert class extension #2077

Closed
MattFoley opened this issue Jul 21, 2015 · 2 comments
Labels
Good first issue Interested in collaborating? Take a stab at fixing one of these issues. Resolution: Locked This issue was locked by the bot.

Comments

@MattFoley
Copy link

I think this section of the docs needs to be further expanded to explain the use of RCT_ENUM_CONVERTER.

In v0.5.0, I was able to export NS_ENUM defined constants, and use them as method arguments. Example code below.

Enum Definition:

    typedef NS_ENUM(NSInteger, MyEnumType)
    {
        kEnumOne,
        kEnumTwo,
        kEnumThree,
    };

Constants:

    - (NSDictionary *)constantsToExport
    {
        return @{@"OnError": @{ @"enumOne" : @(kEnumOne),
                                @"enumTwo"  : @(kEnumTwo),
                                @"enumThree"   : @(kEnumThree)}
                      }
    };

Method Definition:

RCT_EXPORT_METHOD(doSomethingWithEnum:(MyEnumType)enum
                           completion:(RCTResponseSenderBlock)callback)

In v0.7.0, this failed with the following log:

Unknown argument type 'MyEnumType' in method -[MyNativeModule
doSomethingWithEnum:completion:]. Extend RCTConvert to support this type.

I was only able to puzzle the solution out after reading through the internal React Native code for quite some time. (Create a class extension for RCTConvert, use RCT_ENUM_CONVERTER to implement a converter method.)

    @implementation RCTConvert (MyEnumExtension)
    RCT_ENUM_CONVERTER(MyEnumType, (@{ @"enumOne" : @(kEnumOne),
                                       @"enumTwo"  : @(kEnumTwo),
                                       @"enumThree"   : @(kEnumThree)), 
                       kEnumOne, integerValue)
    @end
@brentvatne
Copy link
Collaborator

@MattFoley - thanks for the detailed description! If you can write up the docs for this and submit a PR I'll be happy to review, just ping me on it!

@brentvatne brentvatne added Good first issue Interested in collaborating? Take a stab at fixing one of these issues. Community Responsibility labels Jul 21, 2015
@brentvatne brentvatne changed the title [RCTConvert, Docs] NS_ENUM exported to JS, used as method arguments for native modules, must use RCT_ENUM_CONVERTER in RCTConvert class extension [Docs] NS_ENUM exported to JS, used as method arguments for native modules, must use RCT_ENUM_CONVERTER in RCTConvert class extension Jul 21, 2015
@MattFoley
Copy link
Author

Will do, thanks @brentvatne.

@facebook facebook locked as resolved and limited conversation to collaborators Jul 22, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Good first issue Interested in collaborating? Take a stab at fixing one of these issues. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

3 participants