react-native
react-native copied to clipboard
ReadableMap getCallback (Android)
Any way to get a method of the object passed to native module
NativeModules can take a callback or a promise as an argument.
@ReactMethod
public void doSomething(final ReadableMap options, final Callback callback) {
// do stuff
callback.invoke(result);
}
or
@ReactMethod
public void doSomething(final ReadableMap options, final Promise promise) {
try {
// do stuff
promise.resolve(result);
} catch (Exception e) {
promise.reject(e.getMessage());
}
}
@satya164 I know. But if there're multiple callbacks, it's hard to keep the code tidy. Besides, keeping the arguments in the right order is no easy job. It's common in javascript that wrap the arguments as an object to keep the code clean.
You can write a wrapper on javascript side which passes multiple callbacks to the module, but exposes a simpler API to your app.
like,
MyModule.doStuff = opts => MyNativeModule.doStuff(opts.success, opts.error);
MyModule.doStuff({ success: fn, error: fn });
That's the solution I just worked out! THX!!