简体   繁体   English

ReactNative 在应用程序启动时初始化 Firebase?

[英]ReactNative initialize Firebase at app start?

I have a app.js file, in which I'm creating my NavigationContainer and adding in some screens and calling a function called init (from my firebase.js file, see below) to initialize my app for Firebase.我有一个 app.js 文件,在其中创建我的NavigationContainer并添加一些屏幕并调用名为init的 function (来自我的 firebase.js 文件,见下文)为 Z0354Z89.48D092414FAAF9349 初始化我的应用程序

I also got a LoginScreen.js file, in which I'm handling login / register processes with firebase.我还有一个 LoginScreen.js 文件,我在其中使用 firebase 处理登录/注册过程。 If the user is logged in, the app loads the HomeScreen.js file immediatly.如果用户已登录,应用程序会立即加载 HomeScreen.js 文件。 The firebase config is in a seperate file (firebase.js), which looks like this: 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

I'm now adding a todo list in a new screen the user can navigate to from my HomeScreen.我现在在用户可以从我的主屏幕导航到的新屏幕中添加待办事项列表。 Here I need the db from my firebase.js, because I want to save the objects there.在这里,我需要 firebase.js 中的数据库,因为我想将对象保存在那里。 However, the db seems to be "undefined", probably because I can't access it because I haven't exported it from the firebase.js file.但是,db 似乎是“未定义的”,可能是因为我没有从 firebase.js 文件中导出它,所以我无法访问它。

I tried removing the init function from my app.js, but that throws an "No Firebase App has been created" issue.我尝试从我的 app.js 中删除 init function,但这会引发“未创建 Firebase 应用程序”问题。

Question: How can I initialize my app in the firebase.js, make sure it get's called first AND have a working db exported in the same firebase.js file?问题:如何在 firebase.js 中初始化我的应用程序,确保首先调用它并在同一个 firebase.js 文件中导出工作数据库?

I found a way (just in case this helps anyone in the future): Simply remove the init function and have the app and db exported, like so:我找到了一种方法(以防万一这对将来的任何人有帮助):只需删除 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 };

Then, import app from firebase.js in your app.js file and call it, like so:然后,在 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.

相关问题 如何在 firebase 管理员中初始化多个应用程序 - how to initialize multiple app in firebase admin Firebase SDK for Python:如何使用apiKey初始化App? - Firebase SDK for Python: How to initialize App using apiKey? 无法在 HTML 中的脚本模块内初始化 Firebase APP - Not able to Initialize Firebase APP inside script module in HTML 我如何使用 firebase 初始化我的应用程序 - how do i initialize my app using firebase '默认的 Firebase 应用不存在。 确保通过调用 initialize_app()' 初始化 SDK - 'The default Firebase app does not exist. Make sure to initialize the SDK by calling initialize_app()' 我的 expo 应用程序一直显示 Firebase: No Firebase App [DEFAULT] 即使在我初始化它之后 - My expo app keeps displaying Firebase: No Firebase App [DEFAULT] even after i initialize it 你应该在哪里初始化你的 firebase web 应用程序? - Where are you supposed to initialize your firebase web app? Flutter firebase 未初始化 - Flutter firebase does not initialize 错误默认的 Firebase 应用已经存在。 这意味着您多次调用 initialize_app() 而没有提供应用程序名称作为 - ERROR The default Firebase app already exists. This means you called initialize_app() more than once without providing an app name as the 当我尝试初始化 firebase 应用程序时,为什么 Google Cloud Functions 会抛出“配置 firebase.databaseURL 的无效值”错误? - Why is Google Cloud Functions throwing a "Invalid value for config firebase.databaseURL" error when I try to initialize a firebase app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM