[英]ReactNative initialize Firebase at app start?
我有一个 app.js 文件,在其中创建我的NavigationContainer
并添加一些屏幕并调用名为init
的 function (来自我的 firebase.js 文件,见下文)为 Z0354Z89.48D092414FAAF9349 初始化我的应用程序
我还有一个 LoginScreen.js 文件,我在其中使用 firebase 处理登录/注册过程。 如果用户已登录,应用程序会立即加载 HomeScreen.js 文件。 firebase 配置位于一个单独的文件 (firebase.js) 中,如下所示:
// Import the functions you need from the SDKs you need
import { initializeApp } from 'firebase/app'
import { getDatabase } from 'firebase/database'
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
// Imagine a config file here //
};
const init = () => {
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getDatabase(app);
}
export { init };
// somehow need to export db here
我现在在用户可以从我的主屏幕导航到的新屏幕中添加待办事项列表。 在这里,我需要 firebase.js 中的数据库,因为我想将对象保存在那里。 但是,db 似乎是“未定义的”,可能是因为我没有从 firebase.js 文件中导出它,所以我无法访问它。
我尝试从我的 app.js 中删除 init function,但这会引发“未创建 Firebase 应用程序”问题。
问题:如何在 firebase.js 中初始化我的应用程序,确保首先调用它并在同一个 firebase.js 文件中导出工作数据库?
我找到了一种方法(以防万一这对将来的任何人有帮助):只需删除 init function 并导出应用程序和数据库,如下所示:
// Import the functions you need from the SDKs you need
import { initializeApp } from 'firebase/app'
import { getDatabase } from 'firebase/database'
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
//imagine config here
};
const app = initializeApp(firebaseConfig);
const db = getDatabase(app);
export { app, db };
然后,在 app.js 文件中从 firebase.js 导入app
并调用它,如下所示:
import { app } from './firebase';
{ /* this calls the firebase.js and initializes the app */ }
app
export default function App() { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.