Develop hello world app in React native
Tags: reactnative, beginners, firstapp
Published: 2022-05-22 14:07:15

React Native is a JavaScript framework for building cross-platform applications. It enables web developers to create mobile applications using their existing JavaScript knowledge.

This article is a beginner’s guide to starting your mobile app development journey in React native.

### What is React Native
React Native is an open-source JavaScript framework developed by Meta (Facebook) to build mobile applications for iOS and Android. React framework support is the main advantage of React Native. It enables an easy switch from react.js to react-native where we can reuse almost all concepts we have learned in react.js such as components, props, and states.

Similar to React.js, React Native applications are written using a mixture of JavaScript and XML markup (JSX). The React Native bridge does the native code conversion while building the application. There it calls the native libraries to render the content in Objective-C for iOS and Java for Android. It makes the quite similar to the app we build on native languages and not going to show WebView anymore.

### Why React Native
The existing methods for creating mobile applications using combinations of JavaScript and HTML are rendered using WebViews. It impacts the overall performance of the app thus making it slow to render the content. But React Native renders the contents using the host platform’s standard render APIs. It converts the JSX codes using native rendering APIs and displays it as a native UI element rather than on WebView. React works independently from the main UI thread to maintain high performance without compromising on capability.

Also, React native is quite similar to react.js. Almost all concepts in react.js such as state, prop, components, and routing can be reused in react native without any changes even though it is coded in react.js. Moreover, web developers love JSX injection and react framework support because they can easily make mobile apps with their existing JavaScript knowledge.

## Getting Started
Let’s start with installing dependencies and environment setup.

### Installing dependencies and Environment setup
Node, React Native CLI, JDK, and Android Studio are the basic requirements to start with a react native project. This article is specifically designed for the Windows platform. If you are using any other platforms then it is recommended that you go through the official site for initial setup

[Setting up the development environment · React Native](https://reactnative.dev/docs/environment-setup)

1. Download node.js from the official site and install it with administrator privileges. Make sure that the version is greater than 12. [Click here to download](https://nodejs.org/en/download/)
2. Download openjdk / Java JDK 8 or above, extract it, and add the bin folder path in the environment variables path. [Click here to download](https://jdk.java.net/java-se-ri/8-MR3)
3. Download and install Android studio. Make sure that Android SDK is selected during the installation. [Click here to download](https://developer.android.com/studio/index.html)
4. Configure environment variables. We have to set `ANDROID_HOME` , `JAVA_HOME` , `GRADLE_HOME` variables in the environment variables list. Also add nodejs, openjdk and android platform tools paths in environment variables if not present.
- Open the Windows Control Panel.
- Click on User Accounts, then click User Accounts again.
- Click on Change my environment variables.
- Click on the New… button, provide the variable name as `ANDROID_HOME` and the variable value as the path to your Android SDK. The default location will be `%LOCALAPPDATA%\Android\Sdk`

![path settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iyasx4gx52qmho3b0b9o.png)
- Select Path variable from the User variables list and click on the Edit button. then add the nodejs , openjdk / java jdk bin folder, and android sdk platform-tools folder. Also add the same in System variables.

![path settings 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x96vd1g6wnnknwmksqra.png)
5. React Native is distributed as two npm packages, react-native-cli and react-native. react-native-cli is a lightweight command line interface to build and run the apps whereas react-nativepackage contains the React Native framework code.

Open the command prompt and run the following command to install react native cli,

```
npm install -g react-native-cli
```
6. create-react-native-app is also a command line tool and a boilerplate that makes react native app creation much easier than react-native-cli. So, install create-react-native app in your system.

```
npm install -g create-react-native-app
```

## Create your first project
All react native configurations are completed. So, we can directly move on to create our first app — hello world app.

Open the terminal and type the following command to create a hello world app.

```
create-react-native-app hello-world-app 
cd hello-world-app 
react-native run-android
// or
yarn android
```
![new project 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8lnz82f9qumhikhpje4.png)
![new project 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnqsmxj08lycuqd96pmr.png)
`create-react-app hello-world-app` will setup the default boilerplate for android and iOS development.

`react-native run-android` will start building the app for the android device. After a successful build, we can see an apk file in the output folder. If the device is connected to the machine the app will be installed and opened on that device.

> Try to run the metro server manually from the project folder if the metro server closes automatically during the run and get a metro server down error in the app.

For that, open a new command prompt in the project root folder and run the following command,

```
react-native start
```

![react native start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2397iab3z2cfd118w3qe.png)
![hello world](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv9f0wmjufh06awtui4n.jpeg)
Now, we can do some modifications in the app. So, open the project in VSCode and delete all contents from the app.js file and create a simple functional component with some custom text and style.

```
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default function App() {
  return (
    <View style={styles.container}>
      <Text style={styles.textStyle}>Hello World</Text>
    </View>
  );
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
  textStyle: {
    color: 'blue',
    fontWeight: 'bold',
    fontSize: 30,
    textAlign: 'center'
  }
});
```
There you have it! Your first app in React native :)

![hello world](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rw47r8ds9u87p03xvpzc.jpeg)

If you are trying to run the app on other devices, it might show some compatibility issues. So, you can setup a release version of the app to resolve this kind of issue. You can check to react native site to get to know how to generate a signed apk in react native.

[Publishing to Google Play Store · React Native](https://reactnative.dev/docs/signed-apk-android)

Thanks for reading this article.

If you enjoyed this article, please click on the heart button ♥ and share to help others find it!

If you are interested in further exploring, here are some resources I found helpful along the way:

- [GitHub - codemaker2015/react-native-hello-world: Hello World app in React native](https://github.com/codemaker2015/react-native-hello-world)

- [Setting up the development environment · React Native](https://reactnative.dev/docs/environment-setup)

- [Learn the Basics · React Native](https://reactnative.dev/docs/tutorial)