Last active
August 21, 2017 07:14
-
-
Save martin-cotta/454f7bd1b167077902cbd80b1b6ab92e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { NativeModules } from 'react-native'; | |
const { MyModule } = NativeModules; | |
class MyComponent extends React.Component { | |
componentDidMount() { | |
loadData(); | |
} | |
async loadData() { | |
try { | |
var products = await MyModule.loadProducts(this.props.orderId); | |
// do something with products | |
} catch (e) { | |
console.error(e); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import React | |
@objc(MyModule) | |
class MyModule: RCTEventEmitter { | |
@objc func loadProducts(_ orderId: NSNumber, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) { | |
let products = // load products somehow ... | |
if products { | |
resolve(products); | |
} else { | |
reject(errorCode, errorMessage, error); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import Foundation; | |
#import "RCTBridgeModule.h" | |
@interface RCT_EXTERN_MODULE(MyModule, NSObject) | |
RCT_EXTERN_METHOD(loadProducts:(nonnull NSNumber *)orderId | |
resolve:(RCTPromiseResolveBlock)resolve | |
reject:(RCTPromiseRejectBlock)reject) | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment