React native call native: callback (Android)


In react native, it can be customized in Java layerReactMethod( way, native method in Android can be called directly in JavaScript layer
But in most method calls, we need to know what the result is after calling the method and whether there are exceptions. JavaScript itself is an event driven language. Callback method can be used in JavaScript to process the result returned by function. Similarly, in react native, theCallbackandPromiseInterface to handle the callback of JavaScript calling Java methods.


CallbackIs an interface in react. Bridge, which acts asReactMethodIs used to map the callback function of JavaScript.CallbackThe interface defines only one methodinvoke, invoke accepts multiple parameters, which must be supported in react.bridge.
First, we define a class to call JavaScript

public class StoreModule extends ReactContextBaseJavaModule {

    public String getName() {
        return "StoreModule";


Specify its name asStoreModule
Now we have a way to save ituserNameandpasswordreachShardPreferencesIn this method, we need to define twoCallbackParameter, one is used to deal with the success of the case, one is used to deal with the exception of the case.

    public void addUser(String userName, String password, Callback successCallback, Callback errorCallback) {
        try {
            if (TextUtils.isEmpty(userName)) {
                errorCallback.invoke("user name is empty");
            if (TextUtils.isEmpty(password)) {
                errorCallback.invoke("password is empty");
            preferences.edit().putString(USER_NAME, userName).commit();
            preferences.edit().putString(PASSWORD, password).commit();
            successCallback.invoke("add user success");
        } catch (Exception e) {

Now we have one withCallbackAs a parameterStoreModuleClass, add an instance of this classReactPackageOfcreateNativeModulesThis method can be called in the JavaScript layer.
In JavaScript, call theCallbackMethod of parameter selection:

var {NativeModules}=require('react-native');
var storeModule=NativeModules.StoreModule;

The method callback methods here are anonymous functions that we use. After JavaScript calls Java, the processing result will be changed toCallbackReturn to JavaScript in the form of, and then process the corresponding results in JavaScript.


Promise isES6More friendly API for asynchronous programming and callback added in(,With promise, you can handle callbacks more concisely and flexibly.
stayreact.briageDefined inPromiseThe interface is implementedresolveandrejectHow to do it,resolveIt is used to process the result correctly,rejectUsed to handle abnormal situations.
stayStoreModuleDefine a supportPromiseMethod as parameter:

    public void login(String userName, String password, Promise promise) {
        String storeUserName = preferences.getString(USER_NAME, "");
        String storePassword = preferences.getString(PASSWORD, "");
        if (!equalsString(userName, storeUserName)) {
            promise.reject("0", "user name is wrong");

        if (!equalsString(password, storePassword)) {
            promise.reject("1", "password is wrong");

        WritableMap map = Arguments.createMap();
        map.putDouble("user_id", 1);


thereWritableMapInheritedReadableMapDefines the parameter conversion in Java and JavaScript
When calling this method, a promise object will be returned in JavaScript, which means that you can use JavaScript directly.
Call in JavaScript:


Using promise is better than usingCallbackMore concise, but also more flexible switching between threads.

Code address:

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]