如何在 React JS 中正确导入 firebase 模块来初始化App

cto*_*ife 2 javascript firebase ecmascript-6 reactjs

我正在尝试初始化 Firebase 应用程序,但我不明白如何或在哪里调用它。

我有一个文件名 fire.tsx ,其中有一个对 firebase 的调用initializeApp。但现在怎么办?我如何将其包含在我的应用程序中?

我认为只需添加import { firebaseApp, firebaseAnalytics } from './fire';到 App.tsx 的顶部就可以工作,但显然不行。我这样做错了吗?

import firebase from "firebase";
import "firebase/auth";
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";


const firebaseConfig = {
  apiKey: "xxx-xx-xx",
  authDomain: "xxx-xx-xx",
  databaseURL: "xxx-xx-xx",
  projectId: "xxx-xx-xx",
  storageBucket: "xxx-xx-xx",
  messagingSenderId: "xxx-xx-xx",
  appId: "xxx-xx-xx",
  measurementId: "xxx-xx-xx"
};

const firebaseApp = initializeApp(firebaseConfig); 
export default firebaseApp;

export const firebaseAuth = firebase.auth();

export const firebaseAnalytics = getAnalytics(); 
Run Code Online (Sandbox Code Playgroud)

应用程序.tsx

import { firebaseApp, firebaseAnalytics } from './fire';
Run Code Online (Sandbox Code Playgroud)

Firebase 错误

Dha*_*raj 5

仅使用新的模块化 SDK,因为兼容版本将在未来的更新中删除。尝试这样重构代码:

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


const firebaseConfig = {
  apiKey: "xxx-xx-xx",
  authDomain: "xxx-xx-xx",
  databaseURL: "xxx-xx-xx",
  projectId: "xxx-xx-xx",
  storageBucket: "xxx-xx-xx",
  messagingSenderId: "xxx-xx-xx",
  appId: "xxx-xx-xx",
  measurementId: "xxx-xx-xx"
};

const firebaseApp = initializeApp(firebaseConfig); 

export const firebaseAuth = getAuth(firebaseApp);
export const firebaseAnalytics = getAnalytics(firebaseApp); 
Run Code Online (Sandbox Code Playgroud)

新的模块化 SDK 会检查是否已创建默认实例,因此您不必像以前的版本一样。