0

I'd like to import my module written natively (java, Android) into my React Native sources, in JS.

I have my dependencies loaded into: android/app/libs/dependencies.jar I have created my module into android/app/src/main/java/com/rctcognito/:

Beside some source files, I have the module and the package.

The module is implementing the following class: public class Cognito extends ReactContextBaseJavaModule

And here is the package class:

package com.rctcognito;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RCTCognitoPackage implements ReactPackage {

    @Override
    public List<Class<? extends JavaScriptModule>> createJSModules() {
            return Collections.emptyList();
    }

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
            return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules( ReactApplicationContext reactContext) {
            List<NativeModule> modules = new ArrayList<>();
            modules.add(new Cognito(reactContext));
            return modules;
    }
}

I, then, updated MainApplication.java by adding the new package: import com.rctcognito.RCTCognitoPackage; And adding it in the List: new RCTCognitoPackage(),

Considering to the official doc of React-Native, I should be able to load it like this:

import { NativeModules } from 'react-native';
module.exports = NativeModules.Cognito;
import Cognito from './Cognito';

But my app is not able to load ./Cognito. Would you have any idea what I might be doing wrong please? Thanks a lot!

2 Answers 2

2

That's not how it works.

All native modules are exposed via NativeModules submodule provided by React Native.

This is correct usage.

import { NativeModules } from 'react-native';

const { Cognito } = NativeModules;

Go ahead and do simple console.log(NativeModules) to see, what's exposed from native codebase.

Sign up to request clarification or add additional context in comments.

Comments

0

There are two ways to do that:

Create a file Cognite.js

import {NativeModules} from 'react-native';
module.exports = NativeModules.Cognite;

And then import it where you needed by:

import Cognite from './Cognite';

Another way is to just import directly:

import { NativeModules } from 'react-native';

const { Cognito } = NativeModules;

You can check out React Native Bridge which works in both iOS and Android -> https://medium.com/@abhisheknalwaya/react-native-bridge-for-ios-and-android-43feb9712fcb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.