Interested in implementing direct native to javascript calls in react-native. But didn't find any guides for that.
Please, help with some examples of creating and registering native to javascript modules in react native.
Already figured out that code from official native modules android documentation has a method named createJSModules, which returns a list of JavaScriptModule classes.
class AnExampleReactPackage implements ReactPackage {
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
...
}
Javadoc for com.facebook.react.bridge.JavaScriptModule says:
/**
* Interface denoting that a class is the interface to a module with the same name in JS. Calling
* functions on this interface will result in corresponding methods in JS being called.
* ...
*/
@DoNotStrip
public interface JavaScriptModule {
}
I was able create interface and pass it's class to createJSModules but have no idea how to register appropriate javascript module from js code.
There are two ways to write a native module for your React Native application: Directly within your React Native application’s iOS/Android projects As a NPM package that can be installed as a dependency by your/other React Native applications
However, through native modules, you can write native code that communicates with native calendar APIs. Then you can invoke that native code through JavaScript in your React Native application. In the following sections you will create such a Calendar native module for both Android and iOS.
Better user experience — React Native uses the JavaScript code to render native components from your phone’s OS. In other words, the application’s user interface (UI) is entirely native!
And React.js is a library sometimes called a framework mostly used in Front-end Web Development. There are 80+ Free Resources for Web Designers and Web Developers to learn Web Development and a Beginner guide for Web Developers. So you have to learn React.js to be an expert in React Native. What is there in React.js?
Finally, after looking into react js sources, I figured out the solution, you need to:
Than after react context initialise, you can get it and call via:
ActionsModule jsModule = context.getJSModule(ActionsModule.class); jsModule.callAction("greet", "hello");
That will call your registered js module method asynchronously.
// AppPackage.java
public class AppPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Arrays.<Class<? extends JavaScriptModule>>asList(
ActionsModule.class
);
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
// ActionsModule.java
public interface ActionsModule extends JavaScriptModule {
void callAction(String type, String value);
}
// MyReactActivity.java
public class MyReactActivity extends ReactActivity implements ReactInstanceManager.ReactInstanceEventListener {
...
@Override
public void onReactContextInitialized(ReactContext context) {
ActionsModule jsModule = context.getJSModule(ActionsModule.class);
jsModule.callAction("greet", "hello");
}
...
}
// ActionsModule.js
var ActionsModule = {
callAction(type, value) {
console.log("callAction => " + type + ":" + value);
},
}
module.exports = ActionsModule;
// index.android.js
import {
AppRegistry
} from 'react-native';
import App from './components/App';
// js module registration
var BatchedBridge = require('BatchedBridge');
var ActionsModule = require('./ActionsModule');
BatchedBridge.registerCallableModule(
'ActionsModule',
ActionsModule
);
//~ js module registration
AppRegistry.registerComponent('MyApp', () => App);
UPD> Pushed a demo project to github with a solution for both android and ios: https://github.com/dmba/rct-native2js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With