使用 Facebook 登录时未触发 onLoginFinished 回调

onLoginFinished callback is not fired in Login with Facebook

我正在构建一个简单的 React Native 应用程序,它使用 react-native-fbsdk 实现 "Login with Facebook" 当使用凭据登录时,按钮从 登录 更改为 注销,我可以登录和注销,但是 onLoginFinished 回调永远不会得到已解雇,但 onLogoutFinished 正常工作。

这是我的代码:

import React, { Component } from 'react';
import { View,Alert,Button,Text } from 'react-native';
import { LoginButton, AccessToken } from 'react-native-fbsdk';

export default class Login extends Component {
  constructor(props) {
    super(props);
    this.state = {
      userInfo: null
    }
  }

  onFacebookLoginFinished = (error, result) => {
    if (error) {
      Alert.alert("login has error: " + result.error);
    } else if (result.isCancelled) {
      Alert.alert("login is cancelled.");
    } else {
      AccessToken.getCurrentAccessToken().then(
        (data) => {
          Alert.alert(data.accessToken.toString())
          this.props.navigation.navigate('Home')
        }
      )
    }
  }

  render() {
    return (
      <View>
        <LoginButton
          onLoginFinished={this.onFacebookLoginFinished}
          onLogoutFinished={() => Alert.alert("logout.")}/>
      </View>
    );
  }
};

onLoginFinished 方法更改为:

onLoginFinished={this.onFacebookLoginFinished}

收件人:

onLoginFinished={(error, result) => this.onFacebookLoginFinished(error, result)}

希望对您有所帮助!

你能试试这个代码吗?

尝试自己在函数中完成。

onLoginFinished={(error, data) => {
               if (error) {
      Alert.alert("login has error: " + result.error);
    } else if (result.isCancelled) {
      Alert.alert("login is cancelled.");
    } else {
      AccessToken.getCurrentAccessToken().then(
        (data) => {
          Alert.alert(data.accessToken.toString())
          this.props.navigation.navigate('Home')
        }
      )
    }
          }}

我想你应该试试这个。

onLoginFinished={(error, result) => this.onFacebookLoginFinished(error, result)}

把函数改成这样

onFacebookLoginFinished (error, result){
    ...
}

您正在访问组件实例 (this),因此您需要绑定到您的 onLoginFinished 函数,最好是在您的构造函数中,即

constructor(props) {
  super(props);
  this.state = {
    userInfo: null
  }

  this.onFacebookLoginFinished = this.onFacebookLoginFinished.bind(this);
}

在此处查看更多说明和更多选项:https://reactjs.org/docs/faq-functions.html#bind-in-constructor-es2015