React Native Package Manager包管理器介绍

前端之家收集整理的这篇文章主要介绍了React Native Package Manager包管理器介绍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

React Native Package Manager是为了简化React Native开发而生的包管理器。这个项目的想法来自CocoaPods,fastlane和react-native link。它的目标是让几乎全部可用的模块不需要额外配置就可以使用。
RNPM需要一直在版本控制系统的管理下使用,以便保证一切可以回退。

相关网站

RNPM官方网站
rnpm github
RN组件

需要node4.0以上版本,基本安装和使用方法如下:

# Installation
$ npm install rnpm -g
# Usage
$ npm install react-native-module --save
$ rnpm link react-native-module

下面我们看看实际如下使用rnpm吧,首先你要一个React Native项目啊,
没有就新建一个吧,

react-native init zxtRn

然后到https://js.coach/react-native/ 找需要的模块,以react-native-send-intent 为例吧。
react-native-send-intent模块提供在RN js代码调用Android Intent发送短信,打电话的能力。
有了rnpm我们就不需要通过模块github的说明一步一步配置了,只需要用下面的命令安装就行了。

$ rnpm install react-native-send-intent
react-native-send-intent@1.0.10 node_modules/react-native-send-intent
rnpm-link info Linking react-native-send-intent android dependency
rnpm-link info Android module react-native-send-intent has been successfully linked
rnpm-install info Module react-native-send-intent has been successfully installed & linked

$ git diff
diff --git a/android/app/build.gradle b/android/app/build.gradle
index 2c01c61..69506c5 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -121,6 +121,7 @@ android {
 }

 dependencies {
+ compile project(':react-native-send-intent')
     compile project(':react-native-code-push')
     compile fileTree(dir: "libs",include: ["*.jar"])
     // From node_modules
diff --git a/android/app/src/main/java/com/zxtrn/MainActivity.java b/android/app/src/main/java/com/zxtrn/MainActivity.java
index b8eecad..7e09daf 100644
--- a/android/app/src/main/java/com/zxtrn/MainActivity.java
+++ b/android/app/src/main/java/com/zxtrn/MainActivity.java
@@ -1,6 +1,7 @@
 package com.zxtrn;

 import com.facebook.react.ReactActivity;
+import com.burnweb.rnsendintent.RNSendIntentPackage;
 import com.microsoft.codepush.react.CodePush;
 import com.facebook.react.ReactPackage;
 import com.facebook.react.shell.MainReactPackage;
@@ -45,6 +46,7 @@ public class MainActivity extends ReactActivity {

         return Arrays.<ReactPackage>asList(
             new MainReactPackage(),+ new RNSendIntentPackage(),
             this._codePush
             //new CodePush(this.getResources().getString(R.strings.reactNativeCodePush_androidDeploymentKey),this,BuildConfig.DEBUG)
         );
diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml
index 6252fd9..7b5819b 100644
--- a/android/app/src/main/res/values/strings.xml
+++ b/android/app/src/main/res/values/strings.xml
@@ -1,4 +1,5 @@
 <resources>
+
     <string name="reactNativeCodePush_androidDeploymentKey" moduleConfig="true">UAvQtR1CTph9a9b4axtv3rupnJigVklbZAwzW</string>
     <string name="app_name">zxtRn</string>
     <string name="title_activity_my">MyActivity</string>
diff --git a/android/settings.gradle b/android/settings.gradle
index 19d1a10..ec167d0 100644
--- a/android/settings.gradle
+++ b/android/settings.gradle
@@ -1,5 +1,7 @@
 rootProject.name = 'zxtRn'

 include ':app'
+include ':react-native-send-intent'
+project(':react-native-send-intent').projectDir = new File(rootProject.projectDir,'../node_modules/react-native-send-intent/android')
 include ':react-native-code-push'
 project(':react-native-code-push').projectDir = new File(rootProject.projectDir,'../node_modules/react-native-code-push/android/app')
diff --git a/package.json b/package.json
index 8fd73cc..f7aa8c5 100644
--- a/package.json
+++ b/package.json
@@ -8,6 +8,7 @@
   "dependencies": {
     "react": "^15.0.2","react-native": "^0.26.0",- "react-native-code-push": "^1.11.0-beta"
+ "react-native-code-push": "^1.11.0-beta",
+ "react-native-send-intent": "^1.0.10"
   }
 }
(END)

用git diff可以看到修改文件,如果熟悉gradle和Android就很容易理解发生了什么,就是增加了一个工程依赖,并增加插件包RNSendIntentPackage,依赖的工程是../node_modules/react-native-send-intent/android,
CodePush也可以用rnpm直接安装啊,我的项目上次已经配置好了CodePush。

通过rnpm可以方便的管理RN的第三方模块,不需要做那些繁琐的配置了。

猜你在找的React相关文章