Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call JavaScript function from native code

I have a react-native app and want to integrate it with native SDK. At some point I need to call js function which will return me data, and this call I should make from native side. Here is the flow:

  1. some iOS handler require more info
  2. native layer calls js code
  3. js code calculate/fetch required data
  4. js code return data back to native code
  5. native code receive data and continue working

Is it possible? There is a lib react-native-eval but may be there is more elegant way to do this?

like image 848
Alexei Malashkevich Avatar asked Oct 14 '17 10:10

Alexei Malashkevich


People also ask

Can JavaScript call native code?

Javascript Interface can be helpful when you need to call native code from a WebView on Android.

What does call () do in JavaScript?

The JavaScript call() Method The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

Does JavaScript work with react native?

JavaScript Runtime​ When using React Native, you're going to be running your JavaScript code in two environments: In most cases, React Native will use JavaScriptCore, the JavaScript engine that powers Safari. Note that on iOS, JavaScriptCore does not use JIT due to the absence of writable executable memory in iOS apps.

What does native code mean in JavaScript?

Native code is computer programming (code) that is compiled to run with a particular processor (such as an Intel x86-class processor) and its set of instructions. If the same program is run on a computer with a different processor, software can be provided so that the computer emulates the original processor.


1 Answers

There is currently no clean way of doing this. My suggestion would be to use either a Callback or an Event from native to JS to notify that there is some information needed and to then invoke a function from JS to the native code that gives the required data to the native layer.

like image 154
Daniel Schmidt Avatar answered Oct 01 '22 02:10

Daniel Schmidt