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:
JavaScript
x
8
1
@ReactMethod
2
public String getToken() {
3
String token = "";
4
//then take the token
5
Log.i("getToken:", token);
6
return token;
7
}
8
Then use in js.
JavaScript
1
2
1
var tokenString = thismodule.getToken();
2
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?
Advertisement
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.