Authentication 反应本机| Firebase身份验证|无法触发navigator

Authentication 反应本机| Firebase身份验证|无法触发navigator,authentication,firebase,react-native,firebase-authentication,Authentication,Firebase,React Native,Firebase Authentication,我被这个问题困扰了三天。我最近已经将我的应用程序迁移到Firebase,React Native除了用户身份验证之外,一切都像一个魔咒一样工作。我试图在用户登录时触发导航器。因此,我使用以下方法: componentDidMount() { firebase.auth().onAuthStateChanged(function(user) { if (user) { global.userID = user.uid alert(‘test’)

我被这个问题困扰了三天。我最近已经将我的应用程序迁移到Firebase,React Native除了用户身份验证之外,一切都像一个魔咒一样工作。我试图在用户登录时触发导航器。因此,我使用以下方法:

componentDidMount() {
    firebase.auth().onAuthStateChanged(function(user) {
     if (user) {
        global.userID = user.uid
        alert(‘test’)        

        var naviRef = this.refs.NavRef
        naviRef.push({
          ident: ‘Home’
        })
    } else {
      // No user is signed in.
    }
 });
}

我收到警报(“测试”),但我的导航器什么也不做。如果我将naviRef.push放在«onAuthStateChanged»之外,它就会工作。我不知道我现在能做什么。有比使用导航器更好的解决方案吗?

我认为这应该可以做到:

componentDidMount(){
firebase.auth().onAuthStateChanged((用户)=>{
如果(用户){
global.userID=user.uid
警报(“测试”)
var naviRef=this.refs.NavRef
导航推力({
标识:“家”
})
}否则{
//没有用户登录。
}
});
}

现在,函数中的
this
是正确的,因为arrow函数对其进行词法绑定。因此,您可以正确访问
this.refs.NavRef

我认为这应该可以实现以下目的:

componentDidMount(){
firebase.auth().onAuthStateChanged((用户)=>{
如果(用户){
global.userID=user.uid
警报(“测试”)
var naviRef=this.refs.NavRef
导航推力({
标识:“家”
})
}否则{
//没有用户登录。
}
});
}

现在,函数中的
this
是正确的,因为arrow函数对其进行词法绑定。因此,您可以正确访问
this.refs.NavRef

解决了我的问题!谢谢你解决了我的问题!非常感谢。