Option 1:
The most basic way to create React Native project is using react native cli. React Native CLI can be installed with below command
npm i -g react-native-cli
you can also check the version of installed React Native CLI with below command
react-native -v
You can create a react native project with the below command
react-native init myReactNativeProj
Once the command is finished, you can see the generated React Native project includes the subfolders containing xcode project and android studio project, so the xcode and android studio projects will be used to run and test the project on device in the same way as ordinary native projects. That is way the React Native project created by react-native command requires the native developing environment installed on your local box.
Option 2:
The second option to create React Native project is using create-react-native-app tool. When using this tool, it is assumed that the project will be implemented with pure javascript without the need to link to any custom native libraries. So there is no need to install xcode or android studio on your development box to developing the react native project. In order to run and test the project, the device must install a client side application Expo. The javascript code will be sent to Expo, and Expo will load and parse the javascript code and translate them into native UI controls as well as the related logic. Create-react-native-app tool is the result of the joint effort between React Native and Expo teams
The create-react-native-app tool can be installed with the below command
npm i -g create-react-native-app
You can check the installed create-react-native-app version with the below command
You can create a react native with this option using the below command. The command may ask you to install Expo CLI, when asked, select Yes to install id.
create-react-native-app mySecondApp
After the project is created, you can see the project root directory only contains javascript code, and does not contain the ios or android subfolders as option 1 does.
The advantage of this option is, you do not need to install platform specific development environment in your local box for developing and testing react native project on real device. But the limitation is, you cannot link any native libraries into the project. Note, the local development environment is still needed if you want to test the app on simulator.
You can test the project by running the below command from project's root folder. This will show a QR code and related instruction on both console window and a new browser window.
npm start
To run the app on real device, first install Expo client app on device. Then, for iOS, open camera app to scan the QR code. For android, open Expo app to scan the QR code. The QR code contains the custom scheme of Expo app as below sample
exp://192.168.2.20/19000
To run the app on simulator, you will need have xcode or android studio from the desktop browser, click the related link, it will automatically start the simulator and install Expo client app.
Actually, as an extension of option 2, Expo SDK can also be used to create react native app. Expo Framework provides addition mobile features to React Native, so these features can be easily integrated in React Native projects.
Debug javascript code on real device
In order to debug the javascript code on real device, just shake the device to bring in the debug menu on device, and then clicks "Debug Remote JS" menu item. You can also click "Toggle Element Inspect" to check element information on device. However, to check the element information, it is better to install react dev tool with below command
npm install -g react-devtools
After installing, run the below command will start a React's Electron debugger.
The most basic way to create React Native project is using react native cli. React Native CLI can be installed with below command
npm i -g react-native-cli
you can also check the version of installed React Native CLI with below command
react-native -v
You can create a react native project with the below command
react-native init myReactNativeProj
Once the command is finished, you can see the generated React Native project includes the subfolders containing xcode project and android studio project, so the xcode and android studio projects will be used to run and test the project on device in the same way as ordinary native projects. That is way the React Native project created by react-native command requires the native developing environment installed on your local box.
Option 2:
The second option to create React Native project is using create-react-native-app tool. When using this tool, it is assumed that the project will be implemented with pure javascript without the need to link to any custom native libraries. So there is no need to install xcode or android studio on your development box to developing the react native project. In order to run and test the project, the device must install a client side application Expo. The javascript code will be sent to Expo, and Expo will load and parse the javascript code and translate them into native UI controls as well as the related logic. Create-react-native-app tool is the result of the joint effort between React Native and Expo teams
The create-react-native-app tool can be installed with the below command
npm i -g create-react-native-app
You can check the installed create-react-native-app version with the below command
create-react-native-app --version
You can create a react native with this option using the below command. The command may ask you to install Expo CLI, when asked, select Yes to install id.
create-react-native-app mySecondApp
After the project is created, you can see the project root directory only contains javascript code, and does not contain the ios or android subfolders as option 1 does.
The advantage of this option is, you do not need to install platform specific development environment in your local box for developing and testing react native project on real device. But the limitation is, you cannot link any native libraries into the project. Note, the local development environment is still needed if you want to test the app on simulator.
You can test the project by running the below command from project's root folder. This will show a QR code and related instruction on both console window and a new browser window.
npm start
To run the app on real device, first install Expo client app on device. Then, for iOS, open camera app to scan the QR code. For android, open Expo app to scan the QR code. The QR code contains the custom scheme of Expo app as below sample
exp://192.168.2.20/19000
To run the app on simulator, you will need have xcode or android studio from the desktop browser, click the related link, it will automatically start the simulator and install Expo client app.
Actually, as an extension of option 2, Expo SDK can also be used to create react native app. Expo Framework provides addition mobile features to React Native, so these features can be easily integrated in React Native projects.
Debug javascript code on real device
In order to debug the javascript code on real device, just shake the device to bring in the debug menu on device, and then clicks "Debug Remote JS" menu item. You can also click "Toggle Element Inspect" to check element information on device. However, to check the element information, it is better to install react dev tool with below command
npm install -g react-devtools
After installing, run the below command will start a React's Electron debugger.