如何在React-native中设置firebase onAuthStateChanged侦听器

时间:2019-01-09 10:22:04

标签: reactjs firebase react-native firebase-authentication

我正在为我的项目使用Firebase数据库(Expo适用于react-native)

我了解的是firebase.auth()。onAuthStateChanged侦听用户的登录/注销状态。因此,当用户注销时,应再次触发它。

我不确定在react-native中onAuthStateChanged方法的确切位置。目前,我已将其定位在componentDidMount中,如下所示:

componentDidMount() {
firebase.auth().onAuthStateChanged(user => {
  if (user) {
    console.log("logged in");
  } else {
    console.log("not logged in");
  }
});
}

它在加载组件时起作用,但是问题是当我按退出按钮(在OnPress期间具有firebase.auth()。signOut()方法)时,不会触发onAuthStateChanged方法。

我知道componentDidMount在组件安装时仅加载一次。 那么我应该在哪里找到onAuthStateChanged方法,以便当我按注销时,它将侦听注销操作并再次触发onAuthStateChanged方法?

谢谢。

0 个答案:

没有答案
相关问题