如果用户输入错误密码如何停止导航?

How to Stop Navigation If user enters wrong password?

如果用户输入错误的密码,如何停止导航。我的应用程序在捕获错误后仍在重定向。我试过这个简单的 If 条件,但它似乎不起作用。

login = (email,password)=>{
    try{
        firebase.auth().signInWithEmailAndPassword(email,password)
        .then(function(user){
            console.log(user)
        }); 
    }
    catch(error){
        console.log(error.toString())
if(error===true) { 
    return; //stop the execution of function
}
    }
        this.props.navigation.replace('Home')
}

您的版本是正确的,但 error 不是布尔值,因此您无法检查它是否是 === true 但您可以通过简单地执行 if (error) 来检查它是否存在并且它应该工作

login = (email,password)=>{
    try {
        firebase.auth().signInWithEmailAndPassword(email,password)
        .then(function(user){
            console.log(user)
        }); 
    } catch(error) {
        console.log(error.toString())
        if(error) { 
          return; //stop the execution of function
        }
    }
    this.props.navigation.replace('Home')
}

简单的,就这样吧。。我觉得,没必要写额外的try catch

login = (email,password)=>{

const nav = this.props.navigation;
  
firebase.auth().signInWithEmailAndPassword(email,password)
        .then( user => {
            console.log(user)
             nav.replace('Home')
        }). catch( error => {
             console.log(error) 
       }) 
    
        
}