Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Java Object to React Native Component and Vice versa

I am including react native to my existing android application. I have one activity(say A) that is starting activity B. A activity sends some data in Bundle(consider some Custom Objects POJO) that B needs to render its view. Now I want my Activity B to contain React Native View. How will the objects(POJO) come from Activity A be sent to React Native code**(JavaScipt Code)**?

like image 361
Rahul Avatar asked Jan 26 '26 04:01

Rahul


1 Answers

I know some time has passed, but maybe someone is still looking for an answer.

If you want to send object from JavaScript to ReactNative (let's say as an method argument), which is unfortunately not mentioned in documentation:

let map = {
    name: 'message1',
    surname: 'message2',
}
NativeModules.BluetoothModule.sendObject(map);

And get it in android:

@ReactMethod
public void sendObject(ReadableMap readableMap){
    Log.i(TAG, readableMap.toString());
    //you can decode it here like:
    String name = readableMap.getString("name");
    //or just parse it as json
}

Now for the other way (from Java to Javascript) you can use either Callbacs, Promises or Events. This part is described in documentation here

like image 85
M. Wojcik Avatar answered Jan 28 '26 20:01

M. Wojcik