React Native与Android的基本类型转换

Java和React Native的基本类型对应如下:


Boolean = Bool

Integer、Double、Float = Number

String = String 

Callback = function (React Native回调方式之一)

ReadableMap = Object 

ReadableArray = Array 

WritableMap = Object

WritableArray = Array

重点:ReadableMap、ReadableArray负责接收React Native传递的Object,Array。WritableMap、WritableArray负责为Android发送参数给React Native。用Map、List代替上面的类进行接收和发送,必然会在Android Monitor上看到报错信息。
Android Code:

@ReactMethod
public void reactOnAndroid(ReadableArray readableArray, ReadableMap readableMap, Callback callback) {
        ArrayList arrayList = readableArray.toArrayList();
        HashMap map = readableMap.toHashMap();
        WritableArray writableArray = Arguments.createArray();
        WritableMap writableMap = Arguments.createMap();
        for(Object obj : arrayList){
            writableArray.pushString("" + obj);
        }
        for(String key : map.keySet()){
            writableMap.putString(key,"" + map.get(key));
        }
        callback.invoke(writableArray,writableMap);
    }
 
 

React Native Code:

import {
  NativeModules
} from 'react-native'
let reactAndroid = NativeModules.ReactAndroid
reactAndroid.reactOnAnroid(Array, Object, function (wArray, wMap) {
   window.alert(wArray[0])
   window.alert(JSON.stringify(wMap))
})

你可能感兴趣的:(React Native与Android的基本类型转换)