React & Firebase: Add Firebase to a React App
faraz ahmad
Posted on June 4, 2020
Note: With the release of v9 of the firebase library, this post is out of date.
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
- Go to https://console.firebase.google.com
- Click on
+ Add Project
. - Follow the steps to create your Firebase project.
Register a web app for your Firebase project
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.
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/app";
// Use your config values here.
firebase.initializeApp({
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");
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
rootElement
);
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 (
<div>
<h1>React & Firebase</h1>
<h2>By @farazamiruddin</h2>
<code>
<pre>{JSON.stringify(firebaseApp.options, null, 2)}</pre>
</code>
</div>
);
}
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.
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.
@farazamiruddin
Till next time.
Posted on June 4, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.