Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在js文件中使用firebase类_Reactjs - Fatal编程技术网

Reactjs 如何在js文件中使用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

我目前有一个firebase文件设置,如下所示:

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)}