<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: saransh bhardwaj</title>
    <description>The latest articles on DEV Community by saransh bhardwaj (@saransh_bhardwaj_ac201467).</description>
    <link>https://dev.to/saransh_bhardwaj_ac201467</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1610990%2Fe4845dfc-3a60-4acc-a1e4-1808ce231577.jpg</url>
      <title>DEV Community: saransh bhardwaj</title>
      <link>https://dev.to/saransh_bhardwaj_ac201467</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saransh_bhardwaj_ac201467"/>
    <language>en</language>
    <item>
      <title>Configure Firebase with React</title>
      <dc:creator>saransh bhardwaj</dc:creator>
      <pubDate>Tue, 11 Jun 2024 17:10:11 +0000</pubDate>
      <link>https://dev.to/saransh_bhardwaj_ac201467/configure-firebase-with-react-2mmb</link>
      <guid>https://dev.to/saransh_bhardwaj_ac201467/configure-firebase-with-react-2mmb</guid>
      <description>&lt;p&gt;What is Firebase&lt;/p&gt;

&lt;p&gt;Firebase is a comprehensive mobile and web application development platform provided by Google. It offers a suite of services and tools that facilitate various aspects of app development, including database management, authentication, hosting, cloud functions, and more.&lt;/p&gt;

&lt;p&gt;To set up the project. Let’s use the React boilerplate project — create-react-app.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-react-app react-with-firebase-auth&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To access the app in the browser, run the command&lt;br&gt;
&lt;code&gt;npm start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To begin with, we need to copy the configuration from our Firebase project’s dashboard on their website. The configuration should be pasted as a configuration object in a new file called firebase.js, located in our application’s src/components/Firebase/ directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const config = {
  apiKey: YOUR_API_KEY,
  authDomain: YOUR_AUTH_DOMAIN,
  databaseURL: YOUR_DATABASE_URL,
  projectId: YOUR_PROJECT_ID,
  storageBucket: '',
  messagingSenderId: YOUR_MESSAGING_SENDER_ID,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using create-react-app to set up the application, we can use React environment variables, but we must prefix them with REACT_APP.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const config = {
  apiKey: process.env.REACT_APP_API_KEY,
  authDomain: process.env.REACT_APP_AUTH_DOMAIN,
  databaseURL: process.env.REACT_APP_DATABASE_URL,
  projectId: process.env.REACT_APP_PROJECT_ID,
  storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can have a &lt;code&gt;.env&lt;/code&gt; file in the project’s root folder.&lt;br&gt;
It is a good practice to add this file to &lt;code&gt;.gitignore&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.env&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REACT_APP_API_KEY=*******
REACT_APP_AUTH_DOMAIN=********.firebaseapp.com
REACT_APP_DATABASE_URL=https://********.firebaseio.com
REACT_APP_PROJECT_ID=********
REACT_APP_STORAGE_BUCKET=********.appspot.com
REACT_APP_MESSAGING_SENDER_ID=********
The `Firebase/firebase.js` file would look like this:

import app from 'firebase/app';
const config = {
 apiKey: process.env.REACT_APP_API_KEY,
 authDomain: process.env.REACT_APP_AUTH_DOMAIN,
 databaseURL: process.env.REACT_APP_DATABASE_URL,
 projectId: process.env.REACT_APP_PROJECT_ID,
 storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
 messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
};
class Firebase {
 constructor() {
 app.initializeApp(config);
 }
}
export default Firebase
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: We can have 2 Firebase projects on the Firebase website. One serves the dev environment, and the other serves the production environment.&lt;/p&gt;

&lt;p&gt;In this case, we can have two &lt;code&gt;.env&lt;/code&gt; files:&lt;br&gt;
&lt;code&gt;.env.development&lt;/code&gt; and &lt;code&gt;.env.production&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Firebase setup:&lt;br&gt;
Sign up for an account on the Firebase side and create a project.&lt;/p&gt;

&lt;p&gt;I have set up an app called BridgeCareApp .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbn7m83rv044xm9ufm1ta.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbn7m83rv044xm9ufm1ta.png" alt="Image description" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are creating a web app. So, we will click on the third icon above the text, “Add an app to get started.”&lt;/p&gt;

&lt;p&gt;Once the app is created, the configuration file with APIKey, AuthDomain, etc, will be should. Copy it and save it in a secure location. We plug these values in the .env` files in the react app.&lt;/p&gt;

&lt;p&gt;And that’s it. We have configured Firebase for our React application.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Now, let’s connect Firebase with the react world.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Firebase should be initialized once as a singleton. Exposing the Firebase class to every React component can lead to multiple Firebase instances.&lt;/p&gt;

&lt;p&gt;We will use React’s Context API to provide a Firebase instance once at the top level of our application’s component hierarchy.&lt;/p&gt;

&lt;p&gt;Create a new file named context.js inside the src/components/Firebase directory, and implement the code provided below.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
import React from "react"&lt;/p&gt;

&lt;p&gt;const FirebaseContext = React.createContext(null)&lt;/p&gt;

&lt;p&gt;export default FirebaseContext&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;createContext()&lt;/code&gt; function creates two components we can use in our React app. Firstly, the &lt;code&gt;FirebaseContext.Provider&lt;/code&gt; component provides a Firebase instance at the top level of our React component tree. Secondly, the &lt;code&gt;FirebaseContext.Consumer&lt;/code&gt; component retrieves the Firebase instance in any component if needed.&lt;/p&gt;

&lt;p&gt;We will export all necessary functionalities, such as the Firebase class, Firebase context (for Consumer), and Provider components, in an index.js file located in our Firebase folder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
import FirebaseContext from './context'&lt;br&gt;
import Firebase from './firebase'&lt;/p&gt;

&lt;p&gt;export default Firebase&lt;/p&gt;

&lt;p&gt;export { FirebaseContext }&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using the Firebase class, we can create a Firebase instance and pass it as a value prop to React’s Context.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/index.js&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
import React from 'react';&lt;br&gt;
import ReactDOM from 'react-dom';&lt;br&gt;
import Firebase, { FirebaseContext } from './components/Firebase'&lt;/p&gt;

&lt;p&gt;ReactDOM.render(&lt;br&gt;
  &lt;br&gt;
    &lt;br&gt;
  ,&lt;br&gt;
  document.getElementById('root'),&lt;br&gt;
)&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is an example of using the Firebase instance in any component.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`&lt;br&gt;
import React from 'react'&lt;/p&gt;

&lt;p&gt;import  { FirebaseContext } from '../Firebase'&lt;/p&gt;

&lt;p&gt;const ChildComponent = () =&amp;gt; (&lt;br&gt;
  &lt;br&gt;
    {firebase =&amp;gt; {&lt;br&gt;
      return I can access the firebase&lt;br&gt;
    }}&lt;br&gt;
  &lt;br&gt;
)&lt;/p&gt;

&lt;p&gt;export default ChildComponent&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
Now, Firebase and React are connected!&lt;/p&gt;

</description>
      <category>firebase</category>
      <category>react</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
