创建反应原生模块并在应用程序中使用它 [英] Create react native module and use it in application

查看:69
本文介绍了创建反应原生模块并在应用程序中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 iOS 和 Android 创建 react-native 模块并在应用程序中使用它.

I want to create react-native module for iOS and Android and use it in an application.

我想像本地模块一样使用它,而不是上传到 npm.

I want to use it like a local module, not uploaded to npm.

当我按照官方文档中描述的步骤操作时,我总是在将模块导入 App.js 文件时遇到这些错误:

When i follow the steps described in the official documentation i always have those error on the import of my module into App.js file :

从'react-test-module'导入MyModule;

import MyModule from 'react-test-module';

找不到模块react-test-module"的声明文件.'/Users/dbellerose/Path/to/My/Module/react-test-module/index.js' 隐式具有 'any' 类型.尝试 npm install @types/react-test-modulet 如果它存在或添加一个包含 `declare module 'react-test-module' 的新声明 (.d.ts) 文件;

Could not find a declaration file for module 'react-test-module'. '/Users/dbellerose/Path/to/My/Module/react-test-module/index.js' implicitly has an 'any' type. Try npm install @types/react-test-modulet if it exists or add a new declaration (.d.ts) file containing `declare module 'react-test-module';

当我运行 iOS 应用程序时:

When i run the iOS application :

无法从 /Users/Path/To/My/App/App.js 解析模块 react-test-module:模块 react-test-模块 不存在于 Haste 模块映射中.

Unable to resolve module react-test-module from /Users/Path/To/My/App/App.js: Module react-test-module does not exist in the Haste module map.

该消息给了我一些解决问题的提示,但不起作用.

The message give me some tips to solve the problem, but it does not work.

要解决,请尝试以下操作:1、清除watchman watch:watchman watch-del-all.2、删除node_modules文件夹:rm -rf node_modules &&npm 安装.3. 重置 Metro Bundler 缓存:rm -rf/tmp/metro-bundler-cache-*npm start -- --reset-cache.4.移除加速缓存:rm -rf/tmp/haste-map-react-native-packager-*.

To resolve try the following: 1. Clear watchman watches: watchman watch-del-all. 2. Delete the node_modules folder: rm -rf node_modules && npm install. 3. Reset Metro Bundler cache: rm -rf /tmp/metro-bundler-cache-* or npm start -- --reset-cache. 4. Remove haste cache: rm -rf /tmp/haste-map-react-native-packager-*.

那么您知道在不将包上传到 npm 的情况下创建模块并在应用程序中使用它的好的过程是什么吗?

So what do you know what is the good process to create a module and use it in application without uploading the package to npm ?

最好的问候,

大卫

推荐答案

以下是我可以作为清单与您分享的一些步骤:

Here's some steps I could share with you as a checklist:

  1. yarn global add create-react-native-module
  2. create-react-native-module MyLibrary
  3. CD 到 MyLibrary 文件夹,运行 yarn install
  4. 处理你的模块

要链接到您的项目,您需要将以下内容添加到您的 package.json(路径应该是您的模块所在的位置)

To link to your project, you will need to add the following to your package.json (the path should be wherever your module is sitting at)

"react-native-test-module": "file:../", 
"react-native-my-library": "file:../../react-native-my-library/"

** 请记住,如果您在模块中附加了新方法.您需要:

** Do keep in mind, if you have new methods appended in module. You will need to:

  1. 从您的项目中删除 node_modules 和 Pods
  2. 重新执行yarn/npm install &pod 安装
  1. Remove node_modules and Pods from your project
  2. Re-execute yarn/npm install & pod install

希望有帮助.

这篇关于创建反应原生模块并在应用程序中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆