<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shubham Patil on Medium]]></title>
        <description><![CDATA[Stories by Shubham Patil on Medium]]></description>
        <link>https://medium.com/@pshubham?source=rss-fd6b54f9e835------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dkB9c2xu-i9FRhdVB0Fyyw.jpeg</url>
            <title>Stories by Shubham Patil on Medium</title>
            <link>https://medium.com/@pshubham?source=rss-fd6b54f9e835------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:46:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pshubham/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using React with Cordova]]></title>
            <link>https://medium.com/@pshubham/using-react-with-cordova-f235de698cc3?source=rss-fd6b54f9e835------2</link>
            <guid isPermaLink="false">https://medium.com/p/f235de698cc3</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[cordova]]></category>
            <dc:creator><![CDATA[Shubham Patil]]></dc:creator>
            <pubDate>Mon, 12 Aug 2019 18:28:12 GMT</pubDate>
            <atom:updated>2019-08-17T18:16:37.741Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*POuBXnX3xOJD-BvRyXRPiA.png" /><figcaption><a href="http://www.freepik.com/">Designed by starline / Freepik</a></figcaption></figure><p>In this article I’ll be providing an overview of how you can use react js with cordova to build a hybrid mobile app.</p><h3><strong>Overview</strong></h3><p>Before getting into the details, let’s go over some basic information regarding the frameworks</p><h4><strong>Cordova</strong></h4><p>Apache cordova is a mobile application development framework which can be used to build hybrid (cross platform) mobile apps using web technologies like HTML, Javascript. A single code base can be built to run on different platforms like android, ios and windows.</p><h4><strong>React</strong></h4><p>React is a javascript library used for building user interfaces and has been gaining popularity. It allows you to build reusable and stateful components.</p><p>So let’s get started! I’m assuming that node, npm and cordova is already installed.</p><ol><li>Let’s start by creating a cordova project. Open your terminal and move around to a location of your choice and execute the below command:</li></ol><pre>$ cordova create cordovaReactProject</pre><blockquote>This command creates a folder <strong>cordovaReactProject </strong>containing the cordova project.</blockquote><p>2. Let’s set up a react project using <strong>create-react-app</strong> simultaneously. create-react-app is super easy to use, one command takes care of all the webpack, babel configurations to help you get started.</p><p>If you are using <strong>npm v5.2 +</strong> it comes installed with npx so you can directly use:</p><pre>$ npx create-react-app reactproject</pre><p>For <strong>npm 5.1 and earlier</strong>, create-react-app needs to be installed globally first:</p><pre>$ npm install -g create-react-app<br>$ create-react-app reactproject</pre><blockquote>This creates a folder <strong>reactproject </strong>which contains the react project code base.</blockquote><p><strong>To make sure the react project has setup correctly you can try running npm start within the project directory:</strong></p><pre>$ cd reactproject<br>$ npm start</pre><p>This should start a server and on opening the server url ( <a href="http://localhost:3000/">http://localhost:3000</a> ) in the browser you should see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2aIBdx1-YktlNfWfSNBclg.png" /></figure><p>To summarize we’ve created two seperate project folders cordova and react:</p><ul><li>cordovaReactProject -&gt; folder containing cordova project</li><li>reactproject -&gt; folder containing react project</li></ul><p>3. The <strong>react project</strong> would have a directory structure similar to:</p><pre>├── .gitignore<br>├── package.json<br>├── package-lock.json<br>├── node_modules<br>│   ├──..<br>│   ├──..<br>│   .<br>│   .<br>│   └──<br>├── public<br>│   ├── favicon.ico<br>│   ├── index.html ----&gt; page template<br>│   └── manifest.json<br>├── README.md<br>└── src ----&gt; source files , development directory<br>    ├── App.css<br>    ├── App.js<br>    ├── App.test.js<br>    ├── index.css<br>    ├── index.js ----&gt; starting point<br>    ├── logo.svg<br>    └── serviceWorker.js</pre><p>The package.json file would look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79da07b60a3751f07803d58385b3146c/href">https://medium.com/media/79da07b60a3751f07803d58385b3146c/href</a></iframe><p>Now we need to merge the two projects (Important):</p><ul><li>Copy the <strong>src/</strong> and <strong>public/ </strong>directories from <strong>reactproject</strong> folder to <strong>cordovaReactProject</strong> folder</li><li>The package.json files also need to be merged. Copy the “scripts”, “dependencies” and “browserList” keys from <strong>reactproject/package.json </strong>to <strong>cordovaReactProject/package.json.</strong> Eventually <strong>cordovaReactProject/package.json </strong>should look like:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/baf30999f8098158f07711cbdce0527a/href">https://medium.com/media/baf30999f8098158f07711cbdce0527a/href</a></iframe><p>4. Now that we have merged our <strong>react project</strong> into our <strong>cordova project</strong> the latter would have the following directory structure with <strong>src/</strong> being our main development directory:</p><pre>├── config.xml<br>├── hooks<br>│   └── README.md<br>├── package.json --&gt; <strong>(merged from the react project)</strong><br>├── platforms<br>├── plugins<br>├── public ---&gt; <strong>(copied from the react project)</strong><br>│   ├── favicon.ico<br>│   ├── index.html<br>│   └── manifest.json<br>├── src --&gt; Main working directory <strong>(copied from the react project)</strong><br>│   ├── App.css<br>│   ├── App.js<br>│   ├── App.test.js<br>│   ├── index.css<br>│   ├── index.js<br>│   ├── logo.svg<br>│   └── serviceWorker.js<br>└── www -------&gt; Build directory<br>    ├── css<br>    │   └── index.css<br>    ├── img<br>    │   └── logo.png<br>    ├── index.html<br>    └── js<br>        └── index.js</pre><blockquote>Since package.json was manually updated we need to make sure that the dependencies are installed:</blockquote><pre>$ cd cordovaReactProject/<br>$ npm install</pre><p>To make sure everything has been configured correctly, you can try running <strong>npm start</strong> in the project directory. The server should start and a page resembling the one posted earlier should appear in your browser on the server url.</p><p>5. Now, we need to take care of certain important things:</p><ul><li><strong>Updating the meta tags in our main template page:</strong></li></ul><blockquote>Add the following &lt;meta&gt; tags in the &lt;head&gt; of <strong>public/index.html.</strong></blockquote><pre>&lt;meta http-equiv=&quot;Content-Security-Policy&quot; content=&quot;default-src &#39;self&#39; data: gap: https://ssl.gstatic.com &#39;unsafe-eval&#39; &#39;unsafe-inline&#39;; style-src &#39;self&#39; &#39;unsafe-inline&#39;; media-src *; img-src &#39;self&#39; data: content:;&quot;&gt;</pre><pre>&lt;meta name=&quot;format-detection&quot; content=&quot;telephone=no&quot;&gt;</pre><pre>&lt;meta name=&quot;msapplication-tap-highlight&quot; content=&quot;no&quot;&gt;</pre><pre>&lt;meta name=&quot;viewport&quot; content=&quot;initial-scale=1, width=device-width, viewport-fit=cover&quot;&gt;</pre><ul><li><strong>Loading cordova.js in our main template page:</strong></li></ul><blockquote>Add the following script tag just before &lt;/body&gt; in <strong>public/index.html</strong></blockquote><pre>&lt;script src=&quot;cordova.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</pre><ul><li><strong>Configuring react dom to be loaded after cordova’s ondevice ready event:</strong></li></ul><blockquote>Since <strong>scr/index.js</strong> file is the entry point we need to tweak it a bit so that the react dom loads after the “<strong>deviceready</strong>” event has been fired by cordova.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fbf150de3ac2baabb76fe5c0ddb3500/href">https://medium.com/media/3fbf150de3ac2baabb76fe5c0ddb3500/href</a></iframe><ul><li><strong>Updating “homepage” in package.json:</strong></li></ul><blockquote>The index.html file within the build folder <strong>has absolute paths for loading the assets (js, css files)</strong>. Since the HTML file is going to run directly in webview rather than being hosted on a server the assets need to be accessed using a relative path. For e.g. we want</blockquote><pre>&lt;script src=”./foo/bar.js”&gt;&lt;/script&gt;</pre><p>instead of</p><pre>&lt;script src=&quot;/foo/bar.js&gt;&lt;/script&gt;</pre><p>The solution for this is to add a <strong>“homepage”</strong> property in <strong>package.json as metioned below</strong>. Adding this would make sure that the assets are fetched using a relative path in the <strong>index.html file.</strong></p><pre>&quot;homepage&quot;: &quot;./&quot;</pre><p>6. The code from the <strong>src/</strong> directory written in <strong>JSX and ES6 cannot be directly used as it is inside the cordova app</strong>. So basically the production build of our react app needs to be used to build the cordova app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fd6h-NLf2J2WGQQz6clSUg.png" /><figcaption><a href="http://www.freepik.com/">Designed by starline / Freepik</a></figcaption></figure><p>We can easily automate this process using cordova hooks ( <a href="https://cordova.apache.org/docs/en/latest/guide/appdev/hooks/">more info</a> ). Since we need to execute the <strong>react build command before cordova build is prepared</strong>, we can use the <strong>before_prepare </strong>cordova hook.</p><p>Hooks need to be defined in the project’s config.xml file. Add the below entry under &lt;widget&gt;:</p><pre>&lt;hook type=&quot;before_prepare&quot; src=&quot;scripts/prebuild.js&quot; /&gt;</pre><blockquote>This would execute the <strong>scripts/prebuild.js</strong> before a cordova build starts. Our prebuild.js file would contain a mechanism to execute the react build.</blockquote><blockquote>The advantage of configuring a hook is that you don’t need to worry about platform specific constructs in configuring your tasks e.g. rm -rf, cp won’t work on windows, a folder is accessed as /foo/bar in linux and C:\\foo\\bar in windows.</blockquote><p>7. Now that the hook is defined, create a scripts folder within the project directory i.e. <strong>cordovaReactProject/</strong> in this case. Within the scripts folder create a <strong>prebuild.js </strong>file as well.</p><p>In this file we need to perform two main functions:</p><ul><li>Execute the react build process</li><li>React-scripts generates the build output to the <strong>build/</strong> folder and cordova uses <strong>www/</strong> as its input directory. So once the react build is completed we need to rename <strong>build/</strong> to <strong>www/</strong></li></ul><p>I’ve used an additional rimraf module in my cordova hook, to install that:</p><pre>$ cd cordovaReactProject<br>$ npm i rimraf --save</pre><p>My prebuild.js file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7e634f8791771e4f90b7132217a0dfa/href">https://medium.com/media/e7e634f8791771e4f90b7132217a0dfa/href</a></iframe><p>8. All done! Now we’ll build the cordova app for android to see if everything works!</p><p>Within the cordova project execute ( <strong>Android sdk and build tools must be installed — </strong><a href="https://cordova.apache.org/docs/en/latest/guide/platforms/android/"><strong>Details</strong></a> ):</p><pre>$ cordova platform add android<br>$ cordova run android</pre><p>This would first execute the cordova hook written, then build an apk and run it on an emulator or an android device if connected via usb.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aAGaEnYnC3Ei9Th0JQUgyQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pl1WgrGysTyWatVjvuSVIg.jpeg" /><figcaption>Cordova app android build running on my android phone</figcaption></figure><h3>Some other tips</h3><ul><li>If you are planning to use react-router-dom for routing in the app, use <strong>&lt;HashRouter&gt; instead of &lt;BrowserRouter&gt;</strong></li><li>While accessing the cordova object access it using the window object, or else the react build would throw an undefined variable exception. For .e.g if you are using the local notification plugin ( <a href="https://github.com/katzer/cordova-plugin-local-notifications">plugin link</a> ), instead of using it as:</li></ul><pre>cordova.plugins.notification.local.schedule({<br>    ....<br>});</pre><p>access it using the window object:</p><pre>window.cordova.plugins.notification.local.schedule({<br>    ....<br>});</pre><p>There are other approaches of setting up a project with this configuration, I’ve used this particular approach as seting up a react project from scratch can be a challenging task.</p><p>It involves installing npm modules, creating configurations amongst other things. Doing all of this inside a cordova project adds some complexity. Using create-react-app solves a lot of these problems.</p><p>The source code is available in this github repository — <a href="https://github.com/pshubham95/cordova-react-example">https://github.com/pshubham95/cordova-react-example</a> . You can clone this repository directly to get started.</p><h3>That’s all folks! Happy coding 🍻</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f235de698cc3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>