Tripping
Tripping

Reputation: 453

Should I initialize Firebase on my web for every js file

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

Upvotes: 0

Views: 618

Answers (2)

Mubashir Waheed
Mubashir Waheed

Reputation: 364

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

Upvotes: 1

HexaCrop
HexaCrop

Reputation: 4263

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

Upvotes: 0

Related Questions