Skip to content

React Native : Native modules return nothing

I’m new to React Native and I want to call native modules to get some strings from Android . I write the code like this:

@ReactMethod
public String getToken() {
    String token = "";
    //then take the token
    Log.i("getToken:", token);
    return token;
}

Then use in js.

var tokenString = thismodule.getToken();

However ,when i call the method in js. I can see the correct log ” I/getToken : palapalapala ” in logcat , but js can’t get anything.

So , what’s the correct code of this?

Answer

Oh,yes . I should know . The communicate between js and native is asynchronous . The js method who bridge to the native method , can’t return anything now . So , we must sent a callback function to native and get the correct answer in the callback.

That’s all.