0

I have a React Native App where I am trying to use NativeModules. I wrote a custom class to be used to declare a custom argument type but not sure if I am doing it right. I am new to Java. Here is my MainActivity.java file.

private class JsListener {
    public void onExtensionStarted(){}
    public void onExtensionEnded(){}
}

public class MyModule extends ReactContextBaseJavaModule {

    public JsListener jsListener = new JsListener();

    public CaptureModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @Override
    public String getName() {
        return "MyNativeModule";
    }

    @ReactMethod
    public void startExtension(int clientHandle, JsListener listener){
        ...
    }  


}

And here is where I call the function startExtension in my React Native App.

import React, {Component, useState, useEffect} from 'react';
import {StyleSheet, Text, View, Pressable, NativeModules} from 'react-native';

const { MyNativeModule } = NativeModules;

class JsListener {
  onExtensionStarted(){
    console.log('extension started!')
  }
  onExtensionEnded(){
    console.log('extension ended!')
  }
}

const MyComponent = () =>{

   useEffect(()=>{
      startSocketCamExtension(1111111111)
   }, [])

   const startSocketCamExtension = (handle) =>{
      const jsListener = new JsListener()

      console.log("start")

      MyNativeModule.startSocketCamExtension(handle, jsListener)

      console.log('done')
   }

   return(
      <View>TEST</View>
   )

}

Now for some reason I am getting an error saying Got unknown argument class: JsListener. I declared it in both the Native Modules and the React App to make sure the type was consistent. Any ideas? I am a bit new to Java so I am guessing that maybe I am just declaring or including the classes incorrectly but I am not sure.

Any advice would be greatly appreciated!

Matt Croak
  • 2,788
  • 2
  • 17
  • 35

0 Answers0