1. 程式人生 > >3分鐘完成React-Native熱更新

3分鐘完成React-Native熱更新

此文使用當下最新版本的RNCode-Push進行演示,其中的引數不會過多進行詳細解釋,更多引數解釋可參考其它文章,這裡只保證APP能正常進行熱更新操作,方便快速入門,開始來快活吧。

操作指南

1. 建立React-Native專案

react-native init dounineApp

2. 安裝code-push-cli

npm install -g code-push-cli

3. 註冊code-push帳號

code-push register
Please login to Mobile Center in the browser window we've just opened.

Enter your token from
the browser: #會彈出一個瀏覽器,讓你註冊,可以使用github帳號對其進行授權,授權成功會給一串Token,點選複製,在控制進行貼上回車(或者使用code-push login命令)。
Enter your token from the browser:  b0c9ba1f91dd232xxxxxxxxxxxxxxxxx
#成功提示如下方
Successfully logged-in. Your session file was written to /Users/huanghuanlai/.code-push.config. You can run the code-push logout command at
any time to delete this file and terminate your session.

code-push token

4. 在code-push新增一個ios的app

code-push app add dounineApp-ios ios react-native
#成功提示如下方
Successfully added the "dounineApp-ios" app, along with the following default deployments:
┌────────────┬──────────────────────────────────────────────────────────────────┐
│ Name       │ Deployment Key                                                   │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Production │ yMAPMAjXpfXoTfxCd0Su9c4-U4lU6dec4087-57
cf-4c9d-b0dc-ad38ce431e1d │ ├────────────┼──────────────────────────────────────────────────────────────────┤ │ Staging │ IjC3_iRGEZE8-9ikmBZ4ITJTz9wn6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │ └────────────┴──────────────────────────────────────────────────────────────────┘

5. 繼續在code-push新增一個android的app

code-push app add dounineApp-android android react-native
#成功提示如下方
Successfully added the "dounineApp-android" app, along with the following default deployments:
┌────────────┬──────────────────────────────────────────────────────────────────┐
│ Name       │ Deployment Key                                                   │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Production │ PZVCGLlVW-0FtdoCF-3ZDWLcX58L6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │
├────────────┼──────────────────────────────────────────────────────────────────┤
│ Staging    │ T0NshYi9X8nRkIe_cIRZGbAut90a6dec4087-57cf-4c9d-b0dc-ad38ce431e1d │
└────────────┴──────────────────────────────────────────────────────────────────┘

6. 在專案根目錄新增react-native-code-push

npm install react-native-code-push --save
#或者
yarn add react-native-code-push

7. link react-native-code-push

react-native link
Scanning folders for symlinks in /Users/huanghuanlai/dounine/oschina/dounineApp/node_modules (8ms)
? What is your CodePush deployment key for Android (hit <ENTER> to ignore) T0NshYi9X8nRkIe_cIRZGbAut90a6dec4087-57cf-4c9d-b0dc-ad38ce431e1d

#將剛才新增的Android App的Deployment Key複製貼上到這裡,複製名為Staging測試Deployment Key。

rnpm-install info Linking react-native-code-push android dependency 
rnpm-install info Android module react-native-code-push has been successfully linked 
rnpm-install info Linking react-native-code-push ios dependency 
rnpm-install WARN ERRGROUP Group 'Frameworks' does not exist in your Xcode project. We have created it automatically for you.
rnpm-install info iOS module react-native-code-push has been successfully linked 
Running ios postlink script
? What is your CodePush deployment key for iOS (hit <ENTER> to ignore) IjC3_iRGEZE8-9ikmBZ4ITJTz9wn6dec4087-57cf-4c9d-b0dc-ad38ce431e1d

#繼續複製Ios的Deployment Key

Running android postlink script

8. 在react-nativeApp.js檔案新增自動更新程式碼

...
import codePush from "react-native-code-push";
const codePushOptions = { checkFrequency: codePush.CheckFrequency.MANUAL };
export default class App extends Component<{}> {

  componentDidMount(){
    codePush.sync({
      updateDialog: true,
      installMode: codePush.InstallMode.IMMEDIATE,
      mandatoryInstallMode:codePush.InstallMode.IMMEDIATE,
      //deploymentKey為剛才生成的,打包哪個平臺的App就使用哪個Key,這裡用IOS的打包測試
      deploymentKey: 'IjC3_iRGEZE8-9ikmBZ4ITJTz9wn6dec4087-57cf-4c9d-b0dc-ad38ce431e1d',
      });
  }
  ...

9. 執行專案在ios模擬器上

react-native run-ios

如圖下所顯
1:開啟debug除錯
2:CodePush已經成功執行
目前App已經是最新版本
已經開啟debug的ios

10. 釋出一個ios新版本

code-push release-react dounineApp-ios ios
#釋出成功如圖下
Detecting ios app version:

Using the target binary version value "1.0" from "ios/dounineApp/Info.plist".

Running "react-native bundle" command:

node node_modules/react-native/local-cli/cli.js bundle --assets-dest /var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush --bundle-output /var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush/main.jsbundle --dev false --entry-file index.js --platform ios
Scanning folders for symlinks in /Users/huanghuanlai/dounine/oschina/dounineApp/node_modules (10ms)
Scanning folders for symlinks in /Users/huanghuanlai/dounine/oschina/dounineApp/node_modules (10ms)
Loading dependency graph, done.

bundle: start
bundle: finish
bundle: Writing bundle output to: /var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush/main.jsbundle
bundle: Done writing bundle output

Releasing update contents to CodePush:

Upload progress:[==================================================] 100% 0.0s
Successfully released an update containing the "/var/folders/m_/xcdff0xd62j4l2xbn_nfz00w0000gn/T/CodePush" directory to the "Staging" deployment of the "dounineApp-ios" app.

11. 重新Load重新整理應用
這裡寫圖片描述

12. 安卓釋出
與上面9~11步驟是一樣的,命令改成Android對應的,以下命令結果簡化

#1.修改App.js的deploymentKey為安卓的
...
deploymentKey:'T0NshYi9X8nRkIe_cIRZGbAut90a6dec4087-57cf-4c9d-b0dc-ad38ce431e1d'
...
#2.執行
react-native run-android
#3.釋出
code-push release-react dounineApp-android android
#4.重新整理應用,如下圖

android更新

至此已經完成RN熱更新,進階內容請參考其它文章。