1

So I have a React Native custom library that I made that has 2 modules made, moduleA and moduleB. How do I export them so that I can call both of the modules in my project?

Before when I had only one module it would work perfectly like this:

//Index.js file of my library

import { NativeModules } from 'react-native';
const { moduleA } = NativeModules;
export default moduleA;

and then in my React native project I could call it like this

//Home.js file of my project

import moduleA from 'custom_library';
moduleA.method(); //Works fine

But now that I have two modules, I'm not sure how to change the modules and the import such that both can be used. Is it even possible? The end goal that I want to be able to do is

//Home.js file of my project
import { moduleA, moduleB } from 'custom_library';
moduleA.method();
moduleB.method();

Sorry for the basic question, but I'm getting frustrated that nothing I've tried is really working.

1 Answer 1

2

Just use the export keyword. You will find more detailed examples here.

index.js of custom_library

import { NativeModules } from 'react-native';
const { moduleA, moduleB } = NativeModules;
export { moduleA, moduleB };

home.js

import { moduleA, moduleB } from 'custom_library';
moduleA.method();
moduleB.method();
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry if i wasn't clear, but these are modules whose methods are defined natively in the iOS / Android code. It would be like this facebook.github.io/react-native/docs/…
@AlexanderQian, oh sorry I misunderstood. I updated my answer, have you tried this solution before?

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.