An Introduction To React Native

What is React Native?
 
React Native is a way to write a cross-platform application on JavaScript. If you know JavaScript, then probably you can get started with React Native immediately, because it will provide you with a way to write JavaScript and then turn that JavaScript into your native code.
 
It does not really turn that code into a native code ( you can take a look under the hood) but let's just keep that abstracted. So, let's just assume that React Native somehow does the magic and turns your JavaScript code into native code. And basically, once you are done with that, your application actually runs natively. So, it is not running into a browser or something which some hybrid application frameworks do. React Native actually runs the native code on your device, plus it's super fast to do so and super easy to learn. All you have to know is JavaScript. If you already know JavaScript or you have been working with JavaScript for some time, then you are good to go.
 
Basically, for Android and iOS, the downsides of React Native are that you cannot create every app with it. React Native is sometimes not suitable for developing high-end performance applications like games. In that case, a native application is still the best because you have lots of gears in hand.
 
Getting Started With React Native 
 
Setting Up React Native
 
Go to the official documentation of react-native and click on the "Get Started" button. The confusion starts here. There are two sections -quick start and building project with native code. Most of the people who dive into React Native will go for the quick start section, which is just a couple of lines and good to go. Now for me, building a project with native code is the best option. This runs on Expo client which I don't like and it will limit you in a lot of ways because React Native on its own is like you are writing JavaScript plus you can do a lot of stuff.
 
But when you are using something like Expo, you get bundled, so you are not allowed to move out of a specific room. So, if you want to go with this Expo, you can. However, I would suggest you go with the native code by building projects with native codes.
 
Go ahead with installing node by using the following commands.
 
brew install node 
brew install watchman 
 
Then, install the React Native command line interface by using this command.
 
npm install -g react-native-cli 
 
Now, the React Native setup is completed. If you have Android Studio installed,  it's fine. Otherwise, follow the SDK installation steps in the official documentation. Assuming that you have set up Android Studio, let's start creating our HelloWorld application,
 
react native init <projectName> 
react-native init HelloWorld 
 
This will create a React Native application on the specified location. This project will contain an Android folder and an iOS folder.
 
React Native 
 
There will be one file called App.js. You can open this file in any of the text editors. The content would be like the following.
  1. /** 
  2.  * Sample React Native App 
  3.  * https://github.com/facebook/react-native 
  4.  * 
  5.  * @format 
  6.  * @flow 
  7.  */  
  8. import React, {  
  9.     Component  
  10. } from 'react';  
  11. import {  
  12.     Platform,  
  13.     StyleSheet,  
  14.     Text,  
  15.     View  
  16. } from 'react-native';  
  17. const instructions = Platform.select({  
  18.     ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',  
  19.     android: 'Double tap R on your keyboard to reload,\n' + 'Shake or press menu button for dev menu',  
  20. });  
  21. type Props = {};  
  22. export default class App extends Component < Props > {  
  23.     render() {  
  24.         return ( < View style = {  
  25.             styles.container  
  26.         } > < Text style = {  
  27.             styles.welcome  
  28.         } > Hello World < /Text> < /View>);  
  29.     }  
  30. }  
  31. const styles = StyleSheet.create({  
  32.     container: {  
  33.         flex: 1,  
  34.         justifyContent: 'center',  
  35.         alignItems: 'center',  
  36.         backgroundColor: '#F5FCFF',  
  37.     },  
  38.     welcome: {  
  39.         fontSize: 20,  
  40.         textAlign: 'center',  
  41.         margin: 10,  
  42.     },  
  43.     instructions: {  
  44.         textAlign: 'center',  
  45.         color: '#333333',  
  46.         marginBottom: 5,  
  47.     },  
  48. });  
And the View tag contains the elements which will be shown in the UI. Now, the Hello World application is ready. And you can run the application by using the following command,
 
react-native start,
 
React Native 
 
The main application contains Android and iOS folders; you can open the Android folder in your Android Studio and click Run button and you can see the result in your emulator.
 
React Native 
 
Cheers. Happy coding !!


Similar Articles