React 本机方法未定义

React native method is undefined

我试图在 AccessToken.getCurrentAccessToken() 中调用 signInWithFacebook 方法,但得到 signInWithFacebook 未定义。如果我在 LoginManager.logInWithReadPermissions 函数之外调用它,该方法工作正常。我似乎无法调用 LoginManager.logInWithReadPermissions

中的任何方法

我是 React Native 的新手,请帮忙。谢谢

const FBSDK = require('react-native-fbsdk');
const {
  LoginManager,
  AccessToken,
} = FBSDK;
import React from 'react';
import { Text, View, TouchableOpacity, Image } from 'react-native';

import { connect } from 'react-redux';

import { firebaseActions as auth } from "../actions";
const { signInWithFacebook } = auth;



class FacebookLogin extends React.Component {
  constructor() {
    super();

    this.onSuccess = this.onSuccess.bind(this);
    this.onError = this.onError.bind(this);
    this.onSignInWithFacebook = this.onSignInWithFacebook.bind(this);
  }

  async onSignInWithFacebook() {

    LoginManager.logInWithReadPermissions(["public_profile"]).then(
      function (result) {
        if (result.isCancelled) {
          console.log("Login was Cancelled");
        } else {

          AccessToken.getCurrentAccessToken().then(
            (data) => {
              this.props.signInWithFacebook(data.accessToken, this.onSuccess, this.onError);
            }
          );
        }
      },
      function (error) {
        console.log("An error has occured" + error);
      }
    );

  }

  onSuccess({ exists, user }) {
    console.log(
      "Login Success"
    );

  }

  onError(error) {
    console.log(error.message);
  }

  render() {
    return (
      <View>
        <TouchableOpacity
          onPress={this.onSignInWithFacebook}
        >
          <Text>Login</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

export default connect(null, { signInWithFacebook })(FacebookLogin);

为此只需在您的代码中添加一行:--

async onSignInWithFacebook() {
    const _this = this;
    LoginManager.logInWithReadPermissions(["public_profile"]).then(
      function (result) {
        if (result.isCancelled) {
          console.log("Login was Cancelled");
        } else {

          AccessToken.getCurrentAccessToken().then(
            (data) => {
              _this.props.signInWithFacebook(data.accessToken, this.onSuccess, this.onError);
            }
          );
        }
      },
      function (error) {
        console.log("An error has occured" + error);
      }
    );

  }

在您的代码中,LoginManager 发出一个事件,因此您的 this 范围已更改,您得到了未定义的 signInWithFacebook.. 笔记: - 使用 addEventListener() 附加函数时,此值已更改...

在上一个版本中发生了变化。

logInWithReadPermissionsold react-native-fbsdk 版本 0.8.0 的方法。

logInWithPermissions 应使用自 react-native-fbsdk 版本 0.10.0.