How to Build a React Native App in 2021 📱(Part I)
Tags: reactnative, react, javascript, mobile
Published: 2020-10-27 16:18:30

# Introduction

React Native is a very popular framework for building native mobile applications. Just as [React](https://reactjs.org/), [React Native](https://reactnative.dev/) is unopinionated so you will have to make a lot of implementation decisions. In this article, we will look into what is the best way to build an app depending on your needs.   

## 1. Bootstrap

There are several options to start a new project in React Native:
### **React Native CLI** 

This is the standard tool to create a React Native application.
✅ Pros: 
 1. Highly customizable. It creates two separate applications, one for iOS and one for Android. These are native applications so if you want, you can change the native code.  
 2. You can upgrade to the latest React Native version at any time.
 3. There are no limitations, so you can choose what works best for you.

❌ Cons:
1. The initial setup is a bit complicated. You will need to configure Xcode and Android Studio.
2. Hard to update to a new version of React Native as it requires changes in the native code. 
3. You don't get much out of the box.  

### **Expo CLI** 
Expo is a set of tools built around React Native. It is similar to create-react-app and it has a lot of features.

✅ Pros: 
1. The easiest way to get started. No native code.
2. Lots of features out of the box(Camera, OTA updates, Push Notifications, and many more).
3. Easy to test on a real device. 

❌ Cons:
1. There are some [limitations](https://docs.expo.io/introduction/why-not-expo/).
2. Unless you eject, you cannot change the native code.
3. When a new version of React Native is released, you will have to wait until Expo releases a compatible version. 

_**Conclusion**: There are plenty of cases where it makes sense to use Expo, especially if you are new to React Native. Personally, I think it is a great tool, but it also adds a new layer of complexity. It is worth noticing that you can anytime eject from Expo and have full access to the underlying native projects._

## 2. JavaScript or TypeScript

TypeScript is a language that extends JavaScript, adding type definitions. 
Both React Native CLI and Expo CLI have TypeScript templates.
```bash
# React Native CLI
npx react-native init MyApp --template react-native-template-typescript
```

```bash
# Expo. Select TypeScript template.
expo init MyTSProject
```

Sometimes you will feel that TypeScript slows you down. This is especially because many packages do not have type definitions. But TypeScript is becoming more and more popular and this will be a non-issue in the future. The advantage of using TypeScript is that you catch more errors at compile-time and you also get better code completion.  

_**Conclusion**: If you are building a small app it probably doesn't make sense to use TypeScript. For medium and big applications I would definitely use TypeScript from the beginning._

## 3. Classes vs Functional Components

I see lots of people asking on Stackoverflow whether they should use class based components or functional components and hooks. I will not go into detail here because it is a complex topic, but Dan Abramov explains the benefits of using hooks in [this video](https://www.youtube.com/watch?v=dpw9EHDh2bM&feature=emb_title).

_**Conclusion**: I like to build all my project using functional components and hooks. This is a trend that I see in many new codebases and tutorials. If you feel like using classes, don't worry, there are [no plans](https://reactjs.org/docs/hooks-intro.html#gradual-adoption-strategy) to remove classes from React._  


## 4. State Management

This is HUGE and can have a big impact on development. State management refers to the way you share data across your application. In React, every component can have its own state and data can be passed from a parent component to a child component through props. There is no easy way to pass data between two components which are not in the parent->child relationship. There are many libraries that are trying to resolve this problem, the most popular being Redux.

React 16.3 introduced [Context](https://reactjs.org/docs/context.html) which when combined with hooks can result in a powerful pattern for state management. 

Make sure you check [React State Museum](https://github.com/GantMan/ReactStateMuseum). Here you can find the implementation of a Packing List App using different state management libraries.   

_**Conclusion**: If you are building a simple application you might not need a library like Redux or MobX. They require a lot of boilerplate code and will add a layer of complexity to your app. But if you are going to build a big app, with a lot of data fetching and manipulation and asynchronous actions, make sure you choose the right tool. Redux is still pretty popular, but you should definitely check MobX(especially [MobX State Tree](https://mobx-state-tree.js.org/intro/philosophy))._
         
## 5. UI kit

An UI kit gives you a set of styled UI components which can help you to build your app faster. There are some popular kits out there :
* [React Native Elements](https://reactnativeelements.com/)
* [Native Base](https://nativebase.io/)
* [UI Kitten](https://akveo.github.io/react-native-ui-kitten/)
* [Paper](https://callstack.github.io/react-native-paper/)

_**Conclusion**: It is really handy to use an UI kit and get out of the box components like Footer, Header, or Menu. But I can't stress enough how important is to choose an actively maintained library._ 

## 6. Styling

With React Native you style your application using JavaScript. Every core component accepts a style property. However, inline styles are not recommended since they make the code harder to read.
[Stylesheet](https://reactnative.dev/docs/stylesheet) is a CSS Stylesheet abstraction that lets you move styles away from the render function.
Two powerful libraries are [styled-components](https://styled-components.com/) and [emotion](https://emotion.sh/). They are similar and both are compatible with React Native.

_**Conclusion**: Unlike the web, there are only a few popular options to style React Native applications. It is bad practice to use inline styles so I highly recommend choosing between Stylesheets and Styled Components._    

## 7. Authentication

Most modern apps use some form of authentication. Many applications implement social login(login with Google, Facebook, Apple, or something similar) as it is a fast and secure way to handle authentication.[Expo](https://docs.expo.io/guides/authentication/#google) can be used to login to many providers. Other popular solutions are [Firebase Authentication](https://firebase.google.com/docs/auth) and [AWS Amplify Authentication](https://docs.amplify.aws/lib/auth/getting-started/q/platform/js#option-1-use-pre-built-ui-components).

If you are going to implement authentication against your own backend, you will probably need to persist a JWT or a similar token. Keep in mind that Async Storage is not secure and a better option is to use [React Native Keychain](https://github.com/oblador/react-native-keychain).

_**Conclusion**: If social login is suitable for your needs, I definitely recommend it since it makes your life easier. I personally find Firebase really easy to use. There is also a [package](https://rnfirebase.io/) that bring Firebase support to React Native._

# Summary

This is where we will stop for now.
In the second part of this article, we will discuss Push Notifications, OTA updates, analytics, and many other important features you should definitely add to your app.