Reactjs 如何在js文件中使用firebase类
我目前有一个firebase文件设置,如下所示:Reactjs 如何在js文件中使用firebase类,reactjs,Reactjs,我目前有一个firebase文件设置,如下所示: class Firebase { constructor() { app.initializeApp(firebaseConfig); this.auth = app.auth(); this.db = app.database(); this.storage = app.storage(); } // *** Auth API *** doCreateUserWithEmailAnd
class Firebase {
constructor() {
app.initializeApp(firebaseConfig);
this.auth = app.auth();
this.db = app.database();
this.storage = app.storage();
}
// *** Auth API ***
doCreateUserWithEmailAndPassword = (email, password) =>
this.auth.createUserWithEmailAndPassword(email, password);
这只是文件的一部分。为了访问它,我设置了一个上下文
export const withFirebase = Component => props => (
<FirebaseContext.Consumer>
{firebase => <Component {...props} firebase={firebase} />}
</FirebaseContext.Consumer>
)
在此设置中如何使用firebase类实例
我尝试了const firebaseAuth=new Firebase()代码>
我发现这个错误:Firebase:Firebase应用程序名为“[DEFAULT]”已经存在(应用程序/重复应用程序)。
我解决这个问题的方法是将Firebase作为一个道具传递给动作,我相信这是“反应”方式:
const actions = {
fetchOrg: firebase => async ({ setState }) => {
const callFirebase = await Firebase.auth.onAuthStateChanged(authUser => {
if(authUser) {
//do some stuff
} else {
return false;
}
})
},
}
我的解决方案最终只是将firebase作为行动的道具,我认为这是一种“反应”方式:
const actions = {
fetchOrg: firebase => async ({ setState }) => {
const callFirebase = await Firebase.auth.onAuthStateChanged(authUser => {
if(authUser) {
//do some stuff
} else {
return false;
}
})
},
}
我对此不确定,但您能否尝试constructor(){if(!firebase.apps.length){firebase.initializeApp(config)}
我对此不确定,但您能否尝试constructor(){if(!firebase.apps.length){firebase.initializeApp(config)}