首页 > 解决方案 > 如果用户输入错误的密码,如何停止导航?

问题描述

如果用户输入错误的密码,如何停止导航。发现错误后,我的应用程序仍在重定向。我已经尝试过这个简单的 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')
}

标签: javascriptreactjsreact-nativereact-navigation

解决方案


您的版本是正确的,但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')
}


推荐阅读