简体   繁体   中英

React Native How to pass data from javascript to java android

I followed the tutorial on this site: https://facebook.github.io/react-native/docs/embedded-app-android.html

Now I want to have a function in my javascript code like this:

greetingFunction(){

     return "HELLO, WORLD";
}

I want to pass the string "HELLO, WORLD" to my java android code and set the screen to display that string.

How can I do this?

You should create a custom module to call custom native methods in Java where you can pass the data you need.

For instance if your custom native module implements the following method:

@ReactMethod
public void myMethod(String message) {
  // Here we show a toast message
  Toast.makeText(getReactApplicationContext(), message, Toast.LENGTH_SHORT).show();
}

Then you can call that method from JavaScript:

NativeModules.MyCustomModule.myMethod("HELLO WORLD");

Be sure to properly follow all steps described in the doc above to properly register your custom module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM