React Native for React Developers [Part 3: Setting up Dev Environment]

React Native for React Developers [Part 3: Setting up Dev Environment]

#reactnative #react #newarchitecture #sharingiscaring #KinCarta #WorkingBetter #ForEveryone

Setting up the Development Environment

Before starting with React Native, you need to set up the environment on your machine properly. To do this the right way it is very important to read the official environment setup guide carefully (you can find it here), and don't skip anything, because if you did something wrong, you may need to reinstall everything you have installed, revert everything you have done, and start from scratch.

When you open the official environment setup guide, you will have a guide for Expo and guide for React Native CLI, and make sure you have selected the right one. For Expo it's pretty straight forward, and I wont cover it here, so I will try to help you do the React Native CLI setup the right way, and point you out to the issues that can occur on Apple Silicone Macs, because in my experience I didn't have any difficulties on Windows, Linux or Macs with Intel CPU.

So before you start, I strongly recommend installing this tools:

  • Homebrew (Macs/Linux only): read how to install here.
  • xCode (Macs only): for the latest React Native v0.70, I would suggest to use xCode 14.0.1, since there are some issues with React Native and xCode 14.1.0
  • Android Studio: latest version
  • NVM: It's a Node version manager, follow the installation guide here.
  • RVM: it's a Ruby version manager, follow the installation guide here.

Before we start, let's explain what each tool is, and why we need it.

  • Homebrew: it's a package management system for MacOs and Linux, which will help us with installing some software via the Command Line (Terminal), since manually installation of some tools can be hard, so we don't want to waste time on things like that.
  • xCode: Apple IDE (Integrated Development Environment) for MacOS, used for Software Development on Apple platforms.
  • Android Studio: Official IDE for Android Development, based on JetBrains IntelliJ IDEA.
  • NVM: Node Version Manager. It is used for managing Node versions, which means we can easily install and change node versions, since different projects may use older or newer Node versions, and with the help of NVM we can change them with ease.
  • RVM: Similar like NVM, RVM is a Ruby Version Management, and we need RVM, because different React Native versions require different Ruby versions, also the latest macOS comes with a newer Ruby version, but latest React Native requires older, so we need RVM to install the required Ruby version.

*If you are working on a Mac with Apple Silicone, I would suggest turning Rosetta Mode on for both Terminal and xCode. To do that, in the Finder App, go to the Applications folder, then in the Utilities folder you will find the Terminal app. Right click on it, and select Get Info then check the Open using Rosetta, and do the same for xCode, which you can find in the Applications folder.

After you start following the official guide, the first thing is to install node and watcman, and it is recommended to use homebrew to install them, so in? the terminal app, execute the following commands (make sure you have installed homebrew, and restarting the terminal after installing it):

brew install node
brew install watchman        

iOS Setup

Next it says to install the right Ruby version, and there is a link in the? Guide to see which Ruby version is required for the selected React Native version (on the top of the Guide after the React Native logo you will see a dropdown with React Native version, and the Guide you see, applies for that version of React Native), and install the required version by executing



rvm install 2.7.6
rvm use 2.7.6        

Next follow the Guide and open xCode, and in the preferences select the Command Line tools (in your case the version of the Command Line tools will be different, because it depend of the xCode Version):

No alt text provided for this image

After that you can try to create a new React Native project with NPX, so execute the following command in the terminal (AwesomeProject is the name of your project, so you can pick a different one):



npx react-native init AwesomeProject        

If some errors appear after Downloading the Template, and installing the cocoa pods, you may need to go in the ios folder of your project and installing the pods with the following command:



pod install        

If you still see errors, you may need to switch to a different Ruby version, I recommend 3.1.2, so while in the ios folder in the terminal, execute:



rvm install 3.1.2
rvm use 3.1.2        
No alt text provided for this image
No alt text provided for this image

And try pod install again, and your pods should install after that, and you can run the app, so just go back from the ios folder in the root of the project, and execute:



npx react-native run-ios        

And after a few minutes the metro bundler should start in a new terminal window, after that the simulator will start, with your newly created app running inside the ios Simulator.

If you get error that says that you don't have a simulator with some name installed, or you want to run the app in a different ios Simulator, you can start the simulator by opening xCode, right clicking on it on the Dock, than from Open Developer Tool select Simulator, and on the top of the Simulator app you can see the simulator name, which you can specify like this:



npx react-native run-ios --simulator='iPhone SE (2nd generation)'        
No alt text provided for this image

Android Setup

For Android, you first need to install Java Development kit, and once again we will use homebrew to install that, so execute the following commands in the terminal:



brew tap homebrew/cask-versions
brew install --cask zulu11        

Next we need to install the Android SDK, and we will do that from the Android Studio, so open the Android Studio app, and click on the three dots next to the Open button, and select SDK Manager, and select the Android SDK version specified in the Guide, for me its the latest one and Android SDK 12.0.

*you may also want to install Android SDK Command-line tools, from the SDK tools tab in the SDK Manager.

No alt text provided for this image
No alt text provided for this image

Next you will need to add some system environment variable paths, so we can run the project from the command line. I will use VSCode, and we will need to edit .zshrc file with admin permissions, so from the terminal, execute the following command from the home folder:



sudo code .zshrc        

If you get an error saying that code cannot be found, open VsCode, press cmd+shift+P and type something like shell command, until you see the Shell Command: Install ‘code’ command in PATH and press enter. After that, restart the Terminal, and you should be able to run code inside the terminal.

No alt text provided for this image

After that add those paths in the file:

No alt text provided for this image

Than, save the file, and execute the following command in the terminal:



source .zshrc        

After that just restart, or quit the terminal, and we are done, and we can try running our application, so execute the following command:



npx react-native run-android        

If we have done the setup right, the Android emulator should run after a few minutes, and our app should start in it.

It may show an error saying that the metro bundler is not running, so simply open the terminal app, go inside the root of your project, and run



npm start        

Or yarn start if you are preferring yarn.

After that just reload the app in the emulator (there will be a Reload button below the error in the emulator), or? simply quit and start the app again. You may still get an error, saying to execute some adb command, so go in the terminal and run:



adb reverse:8081 tcp:8081        

After that, reload the app again, and it should work.

If for some reason the app wont build and start from the command line, it means that you have messed up something with the environment setup, and you can try opening the android folder of your app inside Android Studio, wait for gradle to sync (download all the dependencies) and click the green arrow to start the project.

There is a Troubleshooting page, which can help you solve some further issue, you can check it here.

Folder Structure of a React Native project

If you check the newly created React Native project, you will notice multiple files and folders, and in the following text, I will try to explain those files and folders.

  • __tests__: this folder will contain all the test for your application
  • android: this folder will contain the android native app of your project, so if you need to manually link so library, add permissions, custom fonts etc. you will need to make changes in this folder
  • ios: same like the one from above, just this is for iOS.
  • src: this is the root folder for all your source code
  • App.js: the entry component for your app, this component will be mounted first.
  • app.json: this file contains some configuration for your app, like the name, and it is referenced in the index.js
  • babel.config.js: This file contains the configuration for the babel transpiler. If you are not familiar with Babel, in short, it transpiles the React syntax code, to something Javascript engines can understand. You can find out more here.
  • Gemfile: this file contains all the Ruby dependencies needed for your application.
  • Index.js: entry point for your application, here you specify what component to be mounted and rendered first.
  • metro.config.js: this file contains configuration for the metro bundler. You can find out more here.
  • package.json: this file contains all the node dependencies, and some other properties regarding your application. You can find out more here.
  • My common practice is to add the “engine” object inside the package.json, where I specify node and yarn version, so when other developers join the project, and try to run yarn install or npm install, the error will pop up if they don't have the same version installed, and this is to make sure all developer work in the same setup:



"engines": 

???"node": "v16.10.0",

???"yarn": "1.22.19",

???"ruby": "3.1.2"

?},{        

If you are not sure how to structure your new project, here and here you can find some best practices that cover that topic more deeply.


React Native for React Developers [Part 1: React Native and the new Architecture]

React Native for React Developers [Part 2: React Native CLI vs Expo]

React Native for React Developers [Part 3: Setting up Dev Environment]

React Native for React Developers [Part 4: xCode and Android Studio]

React Native for React Developers [Part 5: Core Components, Styling, Platform Specific Code, Permissions and Custom Fonts]

React Native for React Developers [Part 6: Navigation: Stack Navigation]

React Native for React Developers [Part 7: Navigation: Bottom Tab Navigation]

React Native for React Developers [Part 8: Navigation: Deep Links]

React Native for React Developers [Part 9: Push Notifications: iOS]

React Native for React Developers [Part 10: Push Notifications: Android]


要查看或添加评论,请登录

Nikola Gorgiev的更多文章

  • Azure Pipelines hates when Expo dates Firebase

    Azure Pipelines hates when Expo dates Firebase

    The Problem I am not even sure how to start on this article, but I would say that Azure hates when Expo start dating…

    2 条评论
  • Expo + Firebase Push Notifications

    Expo + Firebase Push Notifications

    Here we are with another text, this time related to Push Notifications, and integrating the Firebase Push Notifications…

  • React Native/Expo Azure Pipelines

    React Native/Expo Azure Pipelines

    Some time ago, while I was working on another project with React Native (CLI not Expo), I have written few articles…

  • Како да опстанете во ИТ на долг рок?

    Како да опстанете во ИТ на долг рок?

    После краток период решив повторно да пишувам на една тема, ко?а мислам ?е е доста интересна за чита?е, и претставува…

  • Искуство од првите 10 години во ИТ

    Искуство од првите 10 години во ИТ

    SmileyTech. Пред 10 години и месец дена, офици?ално ?а започнав сво?ата ИТ кариера.

    6 条评论
  • Како до прва работа во ИТ

    Како до прва работа во ИТ

    Во ово? текст ?е пробам со моето искуство да споделам некои совети за оние ко? за прв пат ги креваат едрата во ИТ…

  • Како да успеете во ИТ?

    Како да успеете во ИТ?

    #sharingknowledge #sharingiscaring Вовед Пред да започнам да го пишувам ово? текст, некое време размислував каков да…

    3 条评论
  • Plans for 2024 and 2023 Recap

    Plans for 2024 and 2023 Recap

    Plans for 2024 Полека ?а испративме 2023та и ?а дочекавме новата 2024та. Ко и секо?а година и за оваа решив да направам…

    1 条评论
  • React Native for React Developers [Part 10: Push Notifications: Android]

    React Native for React Developers [Part 10: Push Notifications: Android]

    #reactnative #pushnotifications #sharingiscaring #KinCarta #WorkingBetter #ForEveryone Android Setup For the Android…

  • React Native for React Developers [Part 9: Push Notifications: iOS]

    React Native for React Developers [Part 9: Push Notifications: iOS]

    #reactnative #sharingiscaring #pushnotifications #KinCarta #WorkingBetter #ForEveryone Introduction Push notifications…

社区洞察

其他会员也浏览了