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

标签 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!')
      });
    }

最佳答案

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

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

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

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

希望这会有所帮助!

关于javascript - React Native - Alert.alert 似乎没有调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52249453/

相关文章:

javascript - if instanceof 具有特殊字符的对象

javascript - NVD3.js:如何配置x轴或y轴值

javascript - React - 将平滑滚动到页面部分的导航

react-native - 如何解决 react-native-snap-carousel 中的闪烁图像?

android - 在后台和前台使用 Expo 更改推送通知中的角标(Badge)计数

xcode - react native : How can I create a binary and run it directly on iPhone?

javascript - 在下拉列表更改时显示表中的行

javascript - nodeJS 创建动态数组并用作响应

javascript - 在 redux reducer 中导出默认对象

ios - 不支持的顶级事件类型“在 iphone X 中调度的 onGestureHandlerStateChange