简体   繁体   English

React-Native导入一个库并在多个组件中可用

[英]React-Native import a library and make available in multiple components

How should I import a library/module into React-Native and make it available in multiple components? 我应如何将库/模块导入React-Native并使其在多个组件中可用?

Is there a standard way of doing this without having to import, and configure, the library in every component? 有没有这样做的标准方法,而不必导入和配置每个组件中的库?

I usually handle these situations by creating my own wrapper module that configures and exports the library. 我通常通过创建自己的包装模块来配置和导出库来处理这些情况。 Then when I use it in my app, I use the local wrapper module. 然后,当我在应用程序中使用它时,我将使用本地包装器模块。 For example: 例如:

myLibrary.js myLibrary.js

import library from 'library';

library.configure({ /* options */ });

export default library;

someOtherModule.js someOtherModule.js

import library from './myLibrary';

library.doThings();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM