Home > Software engineering >  Should I initialize Firebase on my web for every js file
Should I initialize Firebase on my web for every js file

Time:03-11

Should i initialize my const firebaseConfig = {}; for every js file?

CodePudding user response:

Nope, just once. If possible on the root js file

CodePudding user response:

You haven't mentioned the type of project you are working on.

Here is how it is done in React project

  1. Create Firebase folder under src in React project
  2. Create config.js in Firebase folder

Initialize the app in config.js and export it

import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";

// Replace the following with your app's Firebase project configuration
const firebaseConfig = {
  apiKey: //your API key,
  authDomain: "random-project.com"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

export {app, db}
 // some other file 
 import { db } from '../Firebase/config';
 const citiesCol = collection(db, 'cities');
 const citySnapshot = await getDocs(citiesCol);

You can read in more depth in official docs here:https://firebase.google.com/docs/web/setup

  • Related