[英]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.