5

I'm writing this to answer my own question because the docs don't explicitly state this and I couldn't find it on stack overflow or anywhere else. Here is how to return a String array from a Java module back into a React Native Component (simplified from the code in my personal project).

CPPConnection.java file

public class CPPConnection extends ReactContextBaseJavaModule {
@ReactMethod
    public void GetDerivedUnits(String scheme,final Promise promise){
        try {

            List<String> returnSet = new ArrayList<String>();
            //Set first value so that there is a lead value
            returnSet.add("");
            returnSet.add(scheme);
            returnSet.add("lb");
            returnSet.add("kg");
            String[] returnArray = new String[returnSet.size()];
            returnArray = returnSet.toArray(returnArray);

            WritableArray promiseArray=Arguments.createArray();
            for(int i=0;i<returnArray.length;i++){
                promiseArray.pushString(returnArray[i]);
            }

            promise.resolve(promiseArray);
        }
        catch(Exception e){
            promise.reject(e);
        }
    }
}

Density.js file

export default class Density extends Component{
...
constructor(props) {
    super(props);
    this.state = {
      mass: '',
      massUnits:[],
      volume: '',
      volumeUnits:[],
      density: 'N/A',
    };
    this.GetDerivedUnits("M",this.state.massUnits);
    this.GetDerivedUnits("L3",this.state.volumeUnits);
}

GetDerivedUnits= async (scheme,unitArray)=>{
    try{

        let asyncUnitSet = await CPPConnection.GetDerivedUnits(scheme);


        for (let i=0;i<asyncUnitSet.length;i++){
            unitArray.push(asyncUnitSet[i])
            this.setState({
                        unitArray: unitArray
                    })

        }

        console.log(asyncUnitSet);
    }catch(e){
        console.error(e);
    }
}
...
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
S. Pan
  • 619
  • 7
  • 13

2 Answers2

15

I'm writing this to answer my own question because the docs don't explicitly state this and I couldn't find it on stack overflow or anywhere else. Here is how to return a String array from a Java module back into a React Native Component (simplified from the code in my personal project).

CPPConnection.java file

public class CPPConnection extends ReactContextBaseJavaModule {
@ReactMethod
    public void GetDerivedUnits(String scheme,final Promise promise){
        try {

            List<String> returnSet = new ArrayList<String>();
            //Set first value so that there is a lead value
            returnSet.add("");
            returnSet.add(scheme);
            returnSet.add("lb");
            returnSet.add("kg");
            String[] returnArray = new String[returnSet.size()];
            returnArray = returnSet.toArray(returnArray);

            WritableArray promiseArray=Arguments.createArray();
            for(int i=0;i<returnArray.length;i++){
                promiseArray.pushString(returnArray[i]);
            }

            promise.resolve(promiseArray);
        }
        catch(Exception e){
            promise.reject(e);
        }
    }
}

Density.js file

export default class Density extends Component{
...
constructor(props) {
    super(props);
    this.state = {
      mass: '',
      massUnits:[],
      volume: '',
      volumeUnits:[],
      density: 'N/A',
    };
    this.GetDerivedUnits("M",this.state.massUnits);
    this.GetDerivedUnits("L3",this.state.volumeUnits);
}

GetDerivedUnits= async (scheme,unitArray)=>{
    try{

        let asyncUnitSet = await CPPConnection.GetDerivedUnits(scheme);


        for (let i=0;i<asyncUnitSet.length;i++){
            unitArray.push(asyncUnitSet[i])
            this.setState({
                        unitArray: unitArray
                    })

        }

        console.log(asyncUnitSet);
    }catch(e){
        console.error(e);
    }
}
...
}
S. Pan
  • 619
  • 7
  • 13
-2

You can use ReadableArray as described here.

Douglas Moreira
  • 130
  • 1
  • 2
  • 10
  • 2
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – clinomaniac Mar 21 '18 at 23:35
  • ReadableArray is an interface. where are other pieces of information? – Ishan Sep 07 '22 at 06:27