Native-to-js bridge has no callback
I'm attempting to call a javascript method from a native module and get some return value or callback , the only thing I have is the RCTBridge property, and the only method I can call to communicate with javascript code is:
- (void)enqueueJSCall:(NSString *)moduleDotMethod args:(NSArray *)args;
which unfortunately has no callback. I saw another method which has callback:
- (void)enqueueApplicationScript:(NSString *)script
url:(NSURL *)url
onComplete:(RCTJavaScriptCompleteBlock)onComplete;
but the comments tell me I should not use it.
So, how to call javascript code and get a return value or callback from native ?
Could you please tell us a little bit more about what you are trying to do?
@frantic , i think @cxfeng1 is trying to invoke JS methods from Objective C and is having trouble doing that as @cxfeng1 cannot find a method to invoke an ObjC callback block.
Oh, I got that, I'm just wondering what higher level task are you trying to solve by calling JS from Obj-C :)
There is no direct access to JS eval because we have an abstraction above it - batched bridge takes care about calling JS code and collecting commands for native side. cc @nicklockwood
@jaygarcia, like @frantic, I'm curious what the high-level problem is, as it may be that there's a simpler or more idiomatic way to do what @cxfeng1 is attempting.
If you want to get data in/out of your JS application then there are abetter ways to do this - e.g. by sending an event from your native module that is monitored by your JS module, and then having your JS code call one of your native module's methods when it's done.
If you just want to call a JS API from native code without wrapping it in the standard React Native framework code then yeah, we don't really support that.
I was considering exposing an API to execute arbitrary JS on the WebView component though, if that's any use to you (it's primarily useful for injecting code into web pages, but it could also be used to execute arbitrary JS code in a sandbox without affecting the main JS application).
@jaygarcia @nicklockwood When a native module event occurs, I just want to know whether JS has required a specific module or implemented a specific method, if YES, I will do some specific work.
Of cource it can be implemented by sending an event to JS and adding listening code in JS, but maybe it is not so intuitive, it have to do a lot of work in JS which I don't want to.
Interesting. So you want a way in native code to know if the JS code is actually using a module?
This seems like a bigger problem at a platform level. Ideally we don't want to be including or executing any native code that isn't needed by the JS app. We've been discussing options such as lazy loading of native modules when they are first called, for example.
Does that sound like the sort of thing that might help with your problem? Perhaps you can give some more details about the sort of work on the native side that you are doing only when the module is required?
@cxfeng1 - can you not just broadcast the event on DeviceEventEmitter or AppEventEmitter?
As @nicklockwood said, if we can get some more details it would help! Closing until we hear back from you, but I am very curious to hear what you have in mind.
I don't like to call the javascript function from React-Native directly. So I ended up I ported WebViewJavascriptBridge to React Native as a separate component, which works fine :D