Cài react native in mac os
React Native Development Environment Installation:With the rapid growth of online websites, businesses, and the general ecosystem, it is crucial that website UIs load quickly on smartphones to encourage smartphone-based internet consumption. Facebook developed React Native from a need to generate UI elements efficiently, which formed the basis for creating the open-source web framework. Its native cross-platform capabilities allow usage for a wide range of platforms for application development, including Android, Web, Windows, UWP, tvOS, macOS, iOS, and AndroidTV. Microsoft also released a preview release in May 2020 that expanded the range of the React Native world, allowing desktop app development. React Native runs in the background of the destination device, and then communicates with the native platform via a batched asynchronous bridge. Its three main advantages are: Show
This article is about how to install and configure React Native CLI environment. Prerequisites
AudienceThis article guides first-time React Native users, junior developers, and developers with no experience with React Native. If curiosity nudged you to learn the framework due to the hype around React, then this is the guide you need. System requirementsSoftware
Hardware
These requirements ensure your tools run quickly and have enough storage for installation and development. System RequirementsReact Native requires macOS High Sierra (10.13) or higher to run efficiently. Installation Through HomeBrew:HomeBrew is a package manager that helps you install and manage packages on Mac, and thus it will be essential to our installation process. You will install HomeBrew so that you can use it to install all the other tools necessary for React Native environment, including Nodejs, git, Watchman, and JDK.
brew—version The command verifies whether HomeBrew has been successfully installed and outputs the specific version installed as the following result shows: homebrew 2.1.7 homebrew/homebrew-core (git revision f487; last commit 2020-05-26)
/usr/bin/ruby -e “$(curl –fsSL https://raw.githubusercontent.com/HomeBrew/install/master/install)”
brew--version If the software is well installed, the result will show the version and date as shown: homebrew 2.1.7 homebrew/homebrew-core(git revision f487; last commit 2020-05-26) NodejsReact Native is a JavaScript platform; hence it needs Nodejs to provide a JavaScript runtime. Nodejs also provides essential tools such as a local webserver with live reloading, network tools, command-line tools, and web APIs.
brew install node
node—version If the installation has occurred successfully, the Nodejs version will show up: V12.6.0
npm—version If the installation is successful, you will see the version as shown below: 6.9.0 WatchmanWatchman monitors the files and folders utilized by the framework and triggers specific actions if the files are modified. It would be best if you had Watchman for React Native to monitor source codes and rebuild them once they are changed.
brew install watchman
watchman --version if properly installed, the result will show the version as shown below: 4.9.0 React Native CLI
npm install --g react-native-cli
react-native --version
If the installation is successful, you will see the version as shown: react-native-cli:2.0.1 react-native:n/a - not inside a React Native Project Xcode
Xcode > Preferences > Locations You should see the Command Line Tools settings showing the version of Xcode as: Xcode 10.2.1 (10E1001)
CocoapodsCoacoapods manages dependencies in iOS development, and thus it is necessary to develop an app by react-native.
sudo gem install cocoapods
pod --version if installation is successful, you can see the version as shown below: 1.7.5 JDK
brew tap AdoptOpenJDK/openjdk brew cask install adoptopenjdk8
java -version If you see the Java version as shown below, then Java has been successfully installed: openjdk version "1.8.0_222" OpenJDK Runtime Environment (AdoptOpenJDK)(BUILD 1.8.0_222-b10) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.222-b10, mixed mode)
javac -version If the installation is successful, the following result shows up: javac 1.8.0_222 Android StudioAndroid Studio is also an essential tool for developing an Android app using react-native. CONFIGURATION
ANDROID STUDIO SDK CONFIGURATION
Select the Show Package Details option and select the following options:
ANDROID STUDIO ENVIRONMENT VARIABLE CONFIGURATION You need to set environment variables to ensure the development environment fits your specifications. Ensure that the ANDROID_HOME environment variable connects to your existing Android SDK to avoid complications. For .bash_profile users, use the following command: source ~/.bash_profile
The correct result if all variables are successfully configured is: Android Debug Bridge Version 1.0.41 version 29.0.1-5644136 Installed as /Your Android SDK Directory Location/platform-tools/adb Git1.You can also use git, which comes with Xcode. However, if its unavailable, run the code below: brew install git git is a substitute for the Android Studio development environment. Gradle DaemonSometimes you may need to modify the java code, in which case, your development speed will be affected. Gradle Daemon is a tool that manages these changes to speed up your development. Other Installation MethodsExpo CLIExpo CLI is a tool built to install and manage react-native frameworks. However, its unsuitable feature is that it has numerous native features, which makes building an app tedious and also makes the app size more extensive than it needs to be. Additionally, incorporating elements in an app that are not available in Expo CLI is difficult, making the tool unsuitable for developers. Despite that, Expo CLI has many useful native tools, unlike React Native CLI, such as geolocation, camera, the microphone that can come in handy for most application needs. Expo CLI is a much easier route than the react-native CLI route. The installation process is as follows: 1.Install the Expo CLI command line utility using: Npm install –g expo-cli 2.Run the command below to create your first project: expo init my app 3. Browse to the directory with the following command in the terminal: 4. Finally, run the project Expo start 5. A development environment server will be started by these commands. Running the app requires installing the Expo client app to your device (iOS/Android) then connecting it to your computer. Android requires scanning the QR code on the terminal to authenticate the project, while iOS requires QR code scanning using the camera app. On the Android (emulator/real device), use the following command to initialize the app: npm run android 6. While on the iOS emulator run: npm run ios Expo has docs that you can reference for solutions or even utilize the Expo Forums created for the Expo community to ask questions. TestingProject StructureReact Native project structure consists of a project directory with configuration files such as package.json, app.json, .watchman.json at the root; separate Android and iOS folders; a _tests_ folder with validation tests for the project; a nodes_module for housing smaller app modules; and an index.js file that maps the application and serves as the initialization point of the application. package.json specifies project dependencies and versions, while app.json contains the app name. The npm (node package manager) manages the node_modules folder, which stores all the project library dependencies. You should avoid modifying the files present in the folder since alterations performed cannot be assured to be stable from updates/installs. Assets in the public folder are to be shared publicly. The src folder contains all development work by the developer, including components and source codes. Developing an App1. First, you need to lock the version you build with to ensure the app still works even after updates, using the following command: npm config set save-exact=true 2. Create a native project through the command: react-native init AwesomeProject cd AwesomeProject react-native run-ios Your new app will run successfully in the emulator. 3. In iOS, execute the command below: cd SampleApp #react-native run ios npm run ios The react-native app will show up on the emulator. 4. On Android, run the following command: cd SampleApp #react-native run-android npm run android Your Android emulator will output: Welcome to React Second App VerificationHello World is a simple program for most development environments. Open index.ios.js for iOS or index.android.js for Android, then proceed to delete everything between the tags The result will be Hello World on the screen. Modifying Your App
Potential Issues
Open and add the following code to the ~/.bash_profile file or the ~/.zshrc file to add the environment variables: #export ANDROID_HOME=$HOME/Library/Android/sdk export ANDROID_HOME=android SDK directory location/ Android/sdk export PATH=$PATH:$ANDROID_HOME/emulator export PATH=$PATH:ANDROID_HOME/tools export PATH=$PATH:$ANDROID_HOME/tools/bin export PATH=$PATH:$ANDROID_HOME/platform-tools For .bash_profile users, use the following command: source ~/.bash_profile Then proceed to verify the correction using the following code: echo $ANDROID_HOME // default: /Users/
Avdmanager list avd Then run Emulator –avd Wait for the device to boot up, then run your app in the emulator React-native run-android Your app should run successfully.
React-native run-android Wrong Android emulator – you need to find the right emulator using the following code: find - -name emulator –type f /Users/ Restart the terminal and verify the error is gone. PATH=$PATH:$ANDROID_HOME/tool s/bin export PATH then run source ~/.bash_profile Quit and restart the terminal, then run the tools. Uninstallation ProcedureSometimes you might need to uninstall and wipe off the whole react-native environment due to issues such as a broken development environment which can be caused by misusing commands or assigning storage to variables incorrectly. To do so efficiently, use the uninstall command together with the -g flag, then include the scope if the package is scoped. A good example is as follows; npm uninstall -g react-native-cli or npm uninstall for scoped package use npm uninstall <@scope/package_name> As our installations have a package.json file, you might need to remove a package from the dependencies in that file. Use the following commands to effect these changes: npm uninstall --save or the following for a scoped package npm uninstall --save <@scope/package_name> You will also need to verify if the uninstallation is successful through the following commands on macOS: ls node_modules This command ensures the node_modules do not contain a folder for the uninstalled files. CAVEAT:
The guide presented has shown how to install React Native through various methods and why the tools stated are required, then showed examples of apps and finally the uninstallation procedure. Tooling and development in React Native are simple, and the learning curve is short, making it an ideal framework for web developers. The growth of internet consumers, cross-platform development, and the whole of the internet ecosystem is an excellent catalyst for React Native to grow in popularity among developers. It offers them faster development speed while offering internet consumers speedier loading times and more beautiful website UIs. As you have seen, React development is simple; hence it should convince you to get started and launch your own apps within a short period. Being a new but highly applicable language, React Native Training holds a lot of opportunities, career wise and development wise. There are also numerous React Native platforms on the internet that can help you solve challenges you might face in development. Keep coding and have fun! |