How To Upgrade React Native To The Latest Version
Tags: reactnative, mobiledev, updates, todayilearned
Published: 2019-12-04 08:55:02

<p><strong>If you have a project in the version older than React Native 0.61</strong>,  you should probably start thinking about upgrading it to the latest one.</p>

<p>However, this is not an easy task. I’ve spent quite a few days with ups and downs, trying many methods, but success seems far away. </p>

<p>Luckily, I got there, and now I can make this process much easier for you.</p>

<p><em>Hint: This tutorial been made for Mac OS X. </em></p>

<h2><b>Two approaches of upgrading to React Native 0.61<b></h2>

<h3><b>1. Upgrading a React Native app in existing projects.</b></h3>

<p><em>Mind that this is not an official guideline prepared by Facebook. The steps from the official documentation didn’t work in my case. </em></p>

<p>The easiest way to upgrade is to do it in the existing project. </p>

<p>Somebody made a fantastic tool named <a href="https://github.com/pvinis/rn-diff-purge">rn-diff-purge</a>. This is a tool that compares different versions of react-native and shows you the differences from the source code level, so it is easy to make changes accordingly to the build. </p>

<p><strong>Steps for upgrading a React Native from version 0.59.5 to  0.61.5:</strong></p>

<ul><li>Go to the diff page (link to diff between RN 0.59.5 and 0.61.5). https://github.com/react-native-community/rn-diff-purge/compare/release/0.59.5..release/0.61.5</li><li> Make changes according to the differences, manually.</li><li> Run yarn/npm to update versions.</li><li> Try to build and run the application.</li></ul>

<p>If there are no errors shown, man you such a lucky person.</p>

<p>If there are some errors, most of them are due to the libraries you are using. </p>

<p>The best way to fix it is to google those issues and look for solutions in Github.</p>

<h3><b>2. Upgrading a React Native app by creating a new project.</b></h3>

<p>The second, the oldest and the best way, for now, is creating a new project in the latest react native version. </p>

<p>After that, you should move all the files from the old repo to the one you have just created. </p>

<p>I will explain to you exactly how to do it at the lowest cost and also how to resolve any errors encountered on the way, so you won’t spend endless hours investigating it.</p>

<h4>Step 1: Upgrade the React Native library</h4>

```javascript
npm install -g react-native@latest
```
<p>That line of code upgrade your React Native global library or install a new one if you did not install react native library before.</p>

<h4>Step 2: Create a new React Native project</h4>

<p>After upgrading the library to the latest version, you have to create a new React Native project. To do this use the command below:</p>

```javascript
react-native init NewApp
```
<img src="https://thepracticaldev.s3.amazonaws.com/i/jchs3wf3tpnpa6lxs451.png" alt="" class="wp-image-2010"/>

<p><strong>“NewApp”</strong> it’s a new RN project name. You can put any name out there if you want.</p>

<h4>Step 3: Install Cocoa Pods</h4>

<p>In this step, you have to get Cocoa pods installed and install the pod created for your iOS project. To make that happen, put the following command to the terminal:</p>

```javascript
sudo gem install cocoapods
```

<p>In the next step, install the pod file generated in your iOS project. You can easily do it by running:</p>

```javascript
cd ios && pod install

```

<p>Assuming you are in your new react native project, you should go to the iOS project first (cd ios) and then install pods. </p>

<p>After running this command you should get a result similar to:</p>


<img src="https://thepracticaldev.s3.amazonaws.com/i/4f8qqi7yzkqed7cup1t1.png" alt="" class="wp-image-2011" width="580" height="154"/>

<h4>Step 4: Upgrade XCode</h4>

<p>If your XCode is in the version below 10, then you should upgrade your XCode to a version that is compatible with your Mac OS. You can do it easily using the App Store.</p>

<p>Now, try to open a new project in upgraded XCode and then try to build the app. </p>

<p>The app should be built successfully at this point. Additionally, try to run a new app using react native CLI. To do that, you should put the following command into your terminal:</p>

```javascript
react-native run-ios
```

<h4>Step 5: Migrate source code</h4>

<p>If your new application works fine, the next step is to move your source code files from your old react native app to the newly created and upgraded one. </p>

<p>Follow the steps below to do it correctly:</p>

<ul><li><strong>Copy your src folder</strong> (assuming your source code is there) to the new app.</li><li><strong>Copy App.js and index.js</strong> from the old to the new one.</li><li><strong>Copy all assets</strong> (like images, icons, splashscreen).</li><li><strong>Install all packages</strong> used in old <code>package.json</code> except react &amp; react-native.</li></ul>

<p>These are the basic ones needed to do a basic code migration. </p>

<p>If you have something more complicated, you might need to copy more configs, code files or do extra installations.</p>

<p>You may also need to go through some files and move the differences between old and new apps. But remember, you must be very careful:</p>

<p><strong>For iOS:</strong></p>

<ul><li><strong>Podfile</strong>- add pods from the old project and again run <strong>pod install</strong></li><li><strong>AppDelegate.m</strong> - add external libraries (e.g. navigation)</li><li><strong>Info.plist</strong></li><li><strong>Trying to enable the same options in XCode as in the old version.</strong></li></ul>

<p><strong>For Android:</strong></p>

<ul><li><strong>settings.gradle</strong> - add repositories</li><li><strong>android/build.gradle</strong></li><li><strong>android/app/build.gradle</strong> - add dependencies</li><li><strong>MainActivity.java</strong></li><li><strong>MainApplication.java</strong> -  add external libraries</li></ul>

<h2><b>Conclusion</b></h2>

<p>There is no ideal way to upgrade react-native. </p>

<p>Steps presented above are just for a simple, and not too complicated apps. For more complex ones, you might obviously encounter more issues. </p>

<p>In this article, I’ve put ways to fix the most common issues. However, most of the problems come with third-party libraries like navigation, camera, upload files, etc. </p>

<p>You can also meet problems with deprecated libraries that work on the old version and haven’t been updated to new ones. </p>

<p>For those issues, there is only one way - look for a reason in Google or GitHub.</p>

<p>If you are planning to use React Native in your next project,<a href="https://pagepro.co/react-native-development.html"> let me assist!</a> </p>