【问题标题】:How to export and use multiple custom modules in the same library with React Native如何使用 React Native 在同一个库中导出和使用多个自定义模块
【发布时间】:2017-08-12 01:58:49
【问题描述】:

所以我制作了一个 React Native 自定义库,其中包含两个模块,moduleAmoduleB。如何导出它们以便我可以调用项目中的两个模块?

在我只有一个模块之前,它会像这样完美地工作:

//Index.js file of my library

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

然后在我的 React 原生项目中我可以这样称呼它

//Home.js file of my project

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

但是现在我有两个模块,我不确定如何更改模块和导入以便两者都可以使用。甚至可能吗?我希望能够做到的最终目标是

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

很抱歉这个基本问题,但我很沮丧,因为我尝试过的任何东西都没有真正奏效。

【问题讨论】:

    标签: node.js reactjs react-native


    【解决方案1】:

    只需使用export 关键字。你会发现更详细的例子here

    custom_library的index.js

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

    home.js

    import { moduleA, moduleB } from 'custom_library';
    moduleA.method();
    moduleB.method();
    

    【讨论】:

    • 如果我不清楚,我很抱歉,但这些模块的方法是在 iOS / Android 代码中本地定义的。会是这样的facebook.github.io/react-native/docs/…
    • @AlexanderQian,抱歉我误会了。我更新了我的答案,你以前试过这个解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2017-05-11
    相关资源
    最近更新 更多