Lately I needed a simple function to interact with the native code, but I decided not to build a package, because it wouldn't be very useful. I created the java files exactly as if they were from a plugin and registered it in MainApplication.
I'm using typescript so now I'm struggling with the rn to java interaction. I tried with a js file as follows:
import NativeModules from 'react-native';
const AndroidService = NativeModules;
export default { AndroidService }
But then I have to define types (message from vs code):
Property 'play' does not exist on type '{ AndroidService: typeof import("/home/karol/Git/TailosiveHub-react/node_modules/@types/react-native/index"); }'.
I tried creating a index.d.ts file in the root of the project, but that doesn't work.
How do I define types for a native module in typescript?
MainApplication:
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new AndroidServicePackage());
// packages.add(new MainReactPackage());
return packages;
}
AndroidServicePackage:
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new AndroidServiceModule(reactContext));
}
AndroidServiceModule:
@Override
public String getName() {
return "AndroidService";
}
Method:
@ReactMethod
public void play(String streamingURL/*, ReadableMap options*/) {
doSomething();
}