React Native-Alert.alert似乎没有调用函数

时间:2018-09-09 23:32:34

标签: javascript react-native

我的注销用户功能似乎根本不起作用。我是从登录警报中调用它的,但似乎没有调用它。如果我尝试在末尾添加“()”,只会给我一个错误。

loginUser = (email, password) => {
      try {
        firebase.auth().signInWithEmailAndPassword(email, password).then(function (user){
          console.log(user)
          console.log('we are logged in boys')
          Alert.alert(
            'Signed In',
            'You have signed in. Well done!',
            [
              {text: 'Sign Out', onPress: () => this.signOutUser},
            ],
            { cancelable: false }
          )
        })
      }
      catch(error) {
        console.log(error.toString())
      }
    }

    signOutUser = () => {
      console.log('Do we reach this or not')
      firebase.auth().signOut().then(function (user){
        // Sign-out successful.
        console.log('We are signing out!')
      }).catch(function(error) {
        // An error happened.
        console.log('There is an issue!')
      });
    }

3 个答案:

答案 0 :(得分:1)

使用箭头功能更新您的功能:

loginUser = (email, password) => {
      try {
        firebase.auth().signInWithEmailAndPassword(email, password).then((user)=> {
          console.log(user)
          console.log('we are logged in boys')
          Alert.alert(
            'Signed In',
            'You have signed in. Well done!',
            [
              {text: 'Sign Out', onPress: () => this.signOutUser()},//Or Write  this.signOutUser direct without arrow function
            ],
            { cancelable: false }
          )
        })
      }
      catch(error) {
        console.log(error.toString())
      }
    }

希望这会有所帮助。

答案 1 :(得分:0)

测试此:

{text: 'Sign Out', onPress: () => { this.signOutUser(); } }

答案 2 :(得分:0)

this回调中的

signInWithEmailAndPassword不是您所期望的。将回调函数更改为arrow function,例如

firebase.auth().signInWithEmailAndPassword(email, password).then((user) => {
  ...
});

此外,将分配的onPress函数更改为

{text: 'Sign Out', onPress: this.signOutUser}

希望这会有所帮助!