Beginners Guide for Integrating Firebase & React Native In An App

Firebase & React Native

The biggest advantage of using React Native for cross-platform mobile development is, a developer can start building MVP with lower development costs and time by using a solution like Firebase. In this article, we will be learning how to get started by integrating Firebase in a React Native application. We will be creating a small application with the help of Firebase & React Native from scratch to see how they work together.
Firebase is a mobile plus web application development platform that was acquired by Google and has a healthy and active community. Most users in this community are mobile developers as Firebase can help with mobile analytics, push notification, crash reporting and out of the box provides email as well as Social authentication. Let us start by creating a new React Native project. I will be using react-native-cli, so make sure you have it already installed using npm in your development environment as a global module. If not, type the following command in your terminal:

npm install -g react-native-cli

To create the project, in the terminal window, type:

react-native init RNFirebaseExample

If the project is successfully initialized, traverse into the project directory using cd RNFirebaseExample. To check, everything is working correctly and our React Native application has been properly initialized run the following command:

react-native run-ios

# For windows users

react-native run-android

To run this command successfully, make sure you have Xcode installed on macOS and you need to run it atleast once. For Windows users, you can install Android Studio by following the instructions from React Native official guide and instead of run-ios type run-android. I will be using run-ios since I am using macOS and for a simulator I will be using iPhone but for android users, you could use android virtual machine and go with the default Nexus emulator device. The complete process slow down once an emulator or a simulator initiates. Once the installation is done, a success message will appear as below.

Firebase installation

Adding Firebase and Initializing Screens

To add Firebase to our existing React Native application, we need to install its dependency:

yarn add firebase

# or

npm install --save firebase

Now let us create the required screens we need for an application to function. First, we will create an src where all the code written for this application will go live. Further, within the src directory, we will create two folders: screens and components. The screen folder will contain all the UI related components that we need to display to the User, whereas the component folder will contain any other component that will be used or re-used to display a UI to the User. In our case, the components directory will contain data from the API.
Our first screen will be a Home Screen. For now, we will display a dummy message within it, screens/home.js:

Our other two screens are going to be AddItem and List. The following is the source code for the other two screens:

Navigating Between Different Screens

In order to navigate the different screens, we created previously, we need to add react-navigation module to our project. This is an open source project that allows us to use a famous way of navigating between the screens or components using Stack based flow. You will notice the same keyword in source code too. First let us install this module:
yarn add react-navigation
After adding this package, I would recommend you to build your application by running react-native run-ios again. After that, open App.js in the root of our project and replace it with the following code:

To make this work, we will edit our Home.js so that we can navigate both the screens from there. We will use Button component from react-native API. react-navigation passes navigation prop to every screen in stack navigator. We have to use same screen name on onPress function to navigate.

navigating the screen

Creating a Database with Firebase & React Native

Go to Firebase Console, login from your Google Account and a create a new project.

create a database

We will be using the databse configuration as a web application. Inside src create a new file called config.js:

Now, we have to fill the details from the Firebase console. In your console, from left sidebar, click on Database and then choose the first option. Select “Start in test mode” and click on enable.

firebase console

To get the configuration, use the left sidebar and click on Project Overview and then Web Application icon. Copy the configured object and paste it in config.js.

Adding Data

We will edit AddItem.js and it, will create a form such that the user can send the application data to firebase when we connect it with the database.

Now, we will add Firebase integration to this component so that when a user clickon the button to add an item, that item gets stored in the database. Before we create a component, import and define:

Also, edit the handleSubmit() function to call this reference to database and display an alert message on the screen.

Adding data

Fetching Items from Database

To fetch data from firebase datastore, we are going to use the same reference to db in List.js:

We also make use of components directory as we create a new ItemComponent.js inside it.

This is how you can integrate Firebase in a React Native application. There is another way to use Firebase database store as an API instead of referring to it, you can directly add in the source code like we did in our application. In that case, you can use the command axios to fetch the data and save it on the Firebase store and handle errors properly since axios is a promise based utility.


Please enter your comment!
Please enter your name here