2017-04-11 13 views
7

私はcreate-react-appでReactJsアプリを作成してから、npm run buildでプロダクションビルドを作成しました。 Cordovaで作成された私のwwwフォルダでは、作成するレスキューアプリのビルドフォルダからすべてのファイルをコピーしても問題ありません。私は例えばのようなコルドバのイベントにフックん方法を知りたいCreate-react-appのコルドバ

:たとえば

function startApp() { 
    // your app logic 
} 
if (window.cordova) { 
    document.addEventListener('deviceready', startApp, false); 
} else { 
    startApp(); 
} 

私はstartApp()の内側に縮小さJSファイルを呼び出したいです。または、反応アプリでCordovaイベントを動作させるために使用できる他のワークフローがありますか?

小さな例が役に立ちます。

ビルドファイルをまったく使用して、React AppをCordovaで直接使用することはできますか? ES6コードをES5に変換するWebpackの設定があるとすれば、どのように動作するのか不明です。

私はCordovaの新機能であり、この統合の面で苦労しています。

+0

私は2つの作業を行う方法を理解しており、同じ回答を探している他の人のためにここに投稿します。それはかなり些細で、おそらく他の方法がありますが、これは私にとってかなりうまくwprkedです。 –

答えて

10

私は2つの仕事をすることが分かっており、同じものを探している他の人のためにここに投稿します。これを行うための他の方法があるかもしれませんが、これが私にとってうまくいきました。

だから、基本的に我々は(と言う)を使用して、コルドバのAppを作成します。

testappとフォルダ内の今
testapp 
     --hooks 
     --platforms 
     --plugins 
     --www 
     --config.xml 

: コルドバは、これがそうと私のフォルダ構造を与えるtestappとのcom.test.testappのtestappと を作成私たちが実行する:create-react-app teastappReact testappフォルダの中に私の反応アプリを追加します。 あなたの反応アプリのメインのindex.jsは/ srcディレクトリにあります。

I index.jsは、関数内で自分のメインロジックをラップして、そのようなコルドバのオブジェクトと一緒に関数を呼び出してください:

今、あなたのアプリが一緒にコルドバのインスタンスを持つことになります行う必要があります
import React from 'react'; 
import ReactDOM from 'react-dom'; 
import App from './App'; 
import './index.css'; 


const startApp =() => { 
ReactDOM.render(
    <App />, 
    document.getElementById('root') 
); 
} 

if(!window.cordova) { 
    startApp() 
} else { 
    document.addEventListener('deviceready', startApp, false) 
} 

あなたのアプリ内にnavigator.cameraのようなDeviceオブジェクトがあります。

パブリックフォルダにあるindex.htmlの反応アプリでも、Codovaのwwwフォルダにあるindex.htmlのhtmlをコピーします。これで、wwwフォルダからすべてのファイルを削除できます。私たちは後で手作業で、または反応アプリのビルドフォルダからCordova wwwフォルダにすべてのファイルをコピーしてスクリプトを使ってコピーします。

私のindex.htmlは以下のようになります。codeova.jsファイルがスクリプトに含まれています。

アプリpackage.jsonは、次の行を追加し反応における最後
<!DOCTYPE html> 
<!-- 
    Licensed to the Apache Software Foundation (ASF) under one 
    or more contributor license agreements. See the NOTICE file 
    distributed with this work for additional information 
    regarding copyright ownership. The ASF licenses this file 
    to you under the Apache License, Version 2.0 (the 
    "License"); you may not use this file except in compliance 
    with the License. You may obtain a copy of the License at 

    http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, 
    software distributed under the License is distributed on an 
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
    KIND, either express or implied. See the License for the 
    specific language governing permissions and limitations 
    under the License. 
--> 
<html> 

<head> 
    <!-- 
     Customize this policy to fit your own app's needs. For more guidance, see: 
      https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy 
     Some notes: 
      * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication 
      * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly 
      * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this: 
       * Enable inline JS: add 'unsafe-inline' to default-src 
     --> 
    <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src * data: content:;"> 
    <meta name="format-detection" content="telephone=no"> 
    <meta name="msapplication-tap-highlight" content="no"> 
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> 
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> 

    <!-- Latest compiled and minified CSS --> 
    <title>React App</title> 
</head> 

<body> 
    <div id="root"></div> 
    <script type="text/javascript" src="cordova.js"></script> 
</body> 

</html> 

: を.... 『ホームページ』:「../www」 .... これはあなたの最終ビルドを確認しますファイルが正しいパスを指しています。 package.jsonビルドスクリプトに次の行を追加することもできます。

"scripts": { 
    "start": "react-scripts start", 
    ***"build": "react-scripts build && robocopy .\\build ..\\www /MIR",*** 
    "test": "react-scripts test --env=jsdom", 
    "eject": "react-scripts eject", 
    "deploy": "npm run build&&gh-pages -d build" 
    } 

これは、OS(Windowsの/ Linuxのなど)に基づいてrobocopyをまたはCP-Rすることができ..

私たちは、コルドバのアプリケーションを cordora build android/iosでビルドする準備ができているはずです。

5

私はこの問題を解決しました。ここで私は解決策を探している人のためのステップバイステップ形式でやったことだ:(create-react-appを使用して作成)

  1. コピー/作成、新しいReactプロジェクトだけCordova appディレクトリ内。
  2. wwwフォルダの内容をすべてCordovaアプリからクリアします。
  3. cdプロジェクトフォルダ(これはコピー/作成したばかりです)&オープンpackage.jsonです。 dependencies
  4. build同じ(React年代)にnpm run buildを行い
  5. "build": "react-scripts build && robocopy .\\build ..\\www /MIR",にを変更するスクリプト内 "homepage": "./", &を追加ディレクトリ&は buildその後、( Cordova)バック親にフォルダを移動して、 emulate希望のプラットフォームでプロジェクト。
  6. また、あなたが<HashRouter>にそうu'll画面に描画されます何として空白の表示を見ること、プロジェクトの変更に<Router>を使用している場合。