Search code examples
reactjsfirebasefirebase-realtime-databasetsx

How to change firebase realtime database URL in React


How can i change database url (firebase real time db) in code? I would like to have option to choose which database you want (just different links)

I have firebaseConfig.ts file where is my firebaseconfig variable that contains apikey, authDomain, databaseURL, ...


Solution

  • https://firebase.google.com/docs/projects/multiprojects#web

    You can have multiple firebase configs in an object like

    var configs = {"option1":{},"option2":{}}
    

    and then initialise them all with different names like

    var databases = {}
    Object.keys(configs).map((name)=>{
      const app = firebase.initializeApp(configs[name],name);
      databases[name]=app;
    })
    

    then based on the choice you can access the database and access the db and change the values etc

    const app = databases["option1"]
    //access the realtime db and so on