0
0
Firebasecloud~5 mins

Firebase with React - Commands & Configuration

Choose your learning style9 modes available
Introduction
Firebase is a service that helps you add backend features like databases and user login to your React app without managing servers. It solves the problem of building and scaling app infrastructure easily.
When you want to add user login and authentication to your React app quickly.
When you need a real-time database to sync data between users instantly.
When you want to store files like images or documents from your React app.
When you want to deploy your React app with hosting included.
When you want to track app usage and errors without setting up your own servers.
Config File - firebaseConfig.js
firebaseConfig.js
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getFirestore } from 'firebase/firestore';

const firebaseConfig = {
  apiKey: "AIzaSyD_example1234567890",
  authDomain: "my-app-12345.firebaseapp.com",
  projectId: "my-app-12345",
  storageBucket: "my-app-12345.appspot.com",
  messagingSenderId: "1234567890",
  appId: "1:1234567890:web:abcdef123456"
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);

export { auth, db };

This file sets up Firebase in your React app.

firebaseConfig holds your project details from Firebase console.

initializeApp starts Firebase with your config.

getAuth and getFirestore create services for login and database.

Exporting auth and db lets you use them in your React components.

Commands
This command installs the Firebase library so you can use Firebase features in your React app.
Terminal
npm install firebase
Expected OutputExpected
+ firebase@10.4.0 added 1 package from 1 contributor and audited 1 package in 1.234s found 0 vulnerabilities
This creates a new React app folder named 'my-firebase-app' where you will add Firebase.
Terminal
npx create-react-app my-firebase-app
Expected OutputExpected
Creating a new React app in /home/user/my-firebase-app. Installing packages. This might take a couple of minutes. Success! Created my-firebase-app at /home/user/my-firebase-app Inside that directory, you can run several commands: npm start npm run build npm test npm eject We suggest that you begin by typing: cd my-firebase-app npm start
Starts the React development server so you can see your app in the browser while you build it.
Terminal
npm start
Expected OutputExpected
Compiled successfully! You can now view my-firebase-app in the browser. Local: http://localhost:3000 On Your Network: http://192.168.1.100:3000 Note that the development build is not optimized. To create a production build, use npm run build.
Builds the React app into static files ready for deployment, including Firebase hosting if configured.
Terminal
npm run build
Expected OutputExpected
Creating an optimized production build... Compiled successfully. File sizes after gzip: 45.2 KB build/static/js/main.abcdef.js 2.1 KB build/static/css/main.abcdef.css The project was built assuming it is hosted at the server root. You can control this with the homepage field in your package.json.
Key Concept

If you remember nothing else from this pattern, remember: Firebase connects your React app to backend services like login and database without needing your own servers.

Common Mistakes
Not installing the Firebase package before importing it in React code.
The app will fail to run because the Firebase library is missing.
Always run 'npm install firebase' before using Firebase in your React app.
Using incorrect Firebase config values copied from the console.
Firebase will not connect properly and features like login or database will fail.
Copy the exact config values from your Firebase project settings and paste them into your config file.
Trying to use Firebase services before calling initializeApp with config.
Firebase services like auth or database won't work without initialization.
Always call initializeApp with your config before using other Firebase features.
Summary
Install Firebase package to use its services in React.
Create a firebaseConfig.js file to initialize Firebase with your project details.
Use npm start to run your React app locally and npm run build to prepare it for deployment.