Cover image for React & Firebase: Add Firebase to a React App

React & Firebase: Add Firebase to a React App

farazamiruddin profile image faraz ahmad ・3 min read

I've been using React together with Firebase for several years now, and I've decided to share some things I've learned along the way. This is my first post in a series which I am titling "React & Firebase". If you are interested in more posts like this, please follow me and share this article. Thanks!

Getting started

All you need is a free Firebase account. To create a Firebase project, you'll need an email address that is powered by Google/Gmail.

Create a Firebase project

Register a web app for your Firebase project

  • Navigate to the Firebase project settings.
    Navigate to the Firebase project settings

  • Select the web platform under Your Apps section.
    Select the web platform under Your Apps section

  • Enter an app name to register your app. Note, we won't be going over Firebase Hosting in this lesson, so you don't have to enable it.

  • Once you finish the steps, you will be presented with your Firebase app's configuration. Copy the configuration values, we will need these when we are initializing Firebase in your React app.
    Copy the configuration values only
    You only need to copy the the configuration object, not the entire snippet, because we will be installing the Firebase client SDK from npm.

Add Firebase to your React app

Install the Firebase SDK via yarn.

yarn add firebase

Initialize your Firebase app

Using the config values you copied in the previous step, you can initialize your Firebase application.

Initialize Firebase in the index.js file.

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import firebase from "firebase";

// Use your config values here.
  apiKey: "AIzaSyDHE9fVBUM_mto-p_SkWnyKtOiRu8M5F98",
  authDomain: "react-firebase-farazamiruddin.firebaseapp.com",
  databaseURL: "https://react-firebase-farazamiruddin.firebaseio.com",
  projectId: "react-firebase-farazamiruddin",
  storageBucket: "react-firebase-farazamiruddin.appspot.com",
  messagingSenderId: "338564911587",
  appId: "1:338564911587:web:c34e6fee0ff41bbe7fd0d6"

const rootElement = document.getElementById("root");
    <App />

Verify the application was initialized correctly

To verify, we can simple display some information about our Firebase app in the browser.

Add the following to the App.js file.

import React from "react";
import firebase from "firebase";

export default function App() {
  const firebaseApp = firebase.apps[0];
  return (
      <h1>React & Firebase</h1>
      <h2>By @farazamiruddin</h2>
        <pre>{JSON.stringify(firebaseApp.options, null, 2)}</pre>

When you initialize a Firebase app, the Firebase will append this app in the apps array. We are grabbing the first item in this array (since we only have one app) and printing out it's options to the browser. These options should match our config values we copied from the Firebase project settings page.

If you followed along, you should see this in your browser.
Displaying our Firebase app's options in the browser

Wrapping up

In this lesson, we went over how to add Firebase to a React app. If you liked this post, please follow me and share it online.

If you'd like to stay in touch, reach out to me on twitter.

Till next time.

Posted on Jun 4 by:

farazamiruddin profile

faraz ahmad


I'm a software engineer with a lot of React and startup experience. I write about my opinions on React, using Firebase with React, and lessons from building my 1st startup, Retro


markdown guide

I really like firebase. It would be great if they would allow us to perform cross-origin API requests in firebase functions.


I like it too! You can do cross-origin requests. You just need to use the cors npm package.

Here's an example I pulled from a function I wrote.

import * as functions from "firebase-functions";
import * as Cors from "cors";

const cors = Cors({ origin: true });

export const handleCrossOriginRequest = functions.https.onRequest((req, res) => {
  cors(req, res, async () => {
    // your logic here

Hope this helps!


Yeah, but you need to set the billing method first.
That's painful.

I see. What do you need cross-origin requests for if I may ask?


Cool article!
I wrote a post that teaches authentication with firebase on the backend


awesome! I was going to post something very similar next :)