反应本机-Facebook登录无法设置状态

反应本机-Facebook登录无法设置状态,facebook,reactjs,react-native,react-native-android,react-native-ios,Facebook,Reactjs,React Native,React Native Android,React Native Ios,我已经在我的项目中完成了Facebook登录功能,但是当登录成功时,我无法setState。我收到此错误this.setState不是函数。我尝试在成功后创建一个单独的函数调用,以setState,结果是相同的,告诉我我的函数不是函数。我该怎么办 constructor(props) { super(props); this.state = { fblogindone:false, }; } _fbAuth(){ // Attempt a

我已经在我的项目中完成了Facebook登录功能,但是当登录成功时,我无法
setState
。我收到此错误
this.setState不是函数
。我尝试在成功后创建一个单独的函数调用,以
setState
,结果是相同的,告诉我我的函数不是函数。我该怎么办

constructor(props) {
    super(props);

    this.state = {
        fblogindone:false,
     };
}


_fbAuth(){
    // Attempt a login using the Facebook login dialog asking for default permissions and email.
    LoginManager.logInWithReadPermissions(['public_profile', 'email']).then(
      function(result) {
        if (result.isCancelled) {
          alert('Login cancelled');
        } else {
          // Create response callback.
          const responseInfoCallback = function(error, result) {
            if (error) {
              console.log(error);
              alert('Error fetching data: ' + error.toString());
            } else {
              console.log(JSON.stringify(result));
              this.setState({
                fblogindone:true
              });
            }
          }
          // Create a graph request asking for user email and names with a callback to handle the response.
          const infoRequest = new GraphRequest('/me',{
              parameters: {
                fields: {
                  string: 'id,name,email'
                }
              }
            },
            responseInfoCallback
          );
          // Start the graph request.
          new GraphRequestManager().addRequest(infoRequest).start()
        }
      },
      function(error) {
        alert('Login fail with error: ' + error);
      }
    );
}

render() {
  return(
      <View style={styles.topcontainer}>
        <TouchableOpacity activeOpacity={0.9} onPress={()=> this._fbAuth()}>
            <FoIcon name="user-circle-o" size={33} color="#fff"/>
        </TouchableOpacity>
      </View>
  );
}
构造函数(道具){
超级(道具);
此.state={
fblogindone:错,
};
}
_fbAuth(){
//尝试使用Facebook登录对话框登录,询问默认权限和电子邮件。
LoginManager.logInWithReadPermissions(['public_profile','email'])。然后(
功能(结果){
如果(结果被取消){
警报(“登录已取消”);
}否则{
//创建响应回调。
const responseInfo回调=函数(错误、结果){
如果(错误){
console.log(错误);
警报('获取数据时出错:'+Error.toString());
}否则{
log(JSON.stringify(result));
这是我的国家({
fblogindone:对
});
}
}
//创建一个图形请求,询问用户电子邮件和姓名,并通过回调处理响应。
const infoRequest=new GraphRequest('/me'{
参数:{
字段:{
字符串:“id、名称、电子邮件”
}
}
},
响应信息回调
);
//启动图形请求。
新建GraphRequestManager().addRequest(infoRequest).start()
}
},
函数(错误){
警报('登录失败,出现错误:'+错误);
}
);
}
render(){
返回(
这是.\u fbAuth()}>
);
}
您必须通过箭头函数或直接绑定将(此)上下文绑定到函数。因此,您将更改函数的声明

_fbAuth(){

您将添加到构造函数中

constructor(props) {
    super(props);

    this.state = {
        fblogindone:false,
     };
    this._fbAuth = this._fbAuth.bind(this)
}

我修正了我自己的错误,所以我发布了我的解决方案,希望以后能对其他人有所帮助。首先,我将函数名更改为
\u fbAuth=()=>{
,然后在子函数中将所有更改返回给arrow函数,
(result)=>{
responseInfo回调=(error,result)=>{
,然后
可以在我的facebook登录回调中使用

    _fbAuth = () =>{
        // Attempt a login using the Facebook login dialog asking for default permissions and email.
        LoginManager.logInWithReadPermissions(['public_profile', 'email']).then(
          (result) => {
            if (result.isCancelled) {
              alert('Login cancelled');
            } else {
              // Create response callback.
              const responseInfoCallback = (error, result) =>{
                if (error) {
                  console.log(error);
                  alert('Error fetching data: ' + error.toString());
                } else {
                  this.setState({
                    fblogindone:true
                  });
                }
              }
              // Create a graph request asking for user email and names with a callback to handle the response.
              const infoRequest = new GraphRequest('/me',{
                  parameters: {
                    fields: {
                      string: 'id,name,email'
                    }
                  }
                },
                responseInfoCallback
              );
              // Start the graph request.
              new GraphRequestManager().addRequest(infoRequest).start()
            }
          },
          function(error) {
            alert('Login fail with error: ' + error);
          }
        );
    }

使用
\u fbAuth=()=>{
this.\u fbAuth=this.\u fbAuth.bind(this)
也有相同的结果
this.setState不是一个函数
我回答我自己的问题
    _fbAuth = () =>{
        // Attempt a login using the Facebook login dialog asking for default permissions and email.
        LoginManager.logInWithReadPermissions(['public_profile', 'email']).then(
          (result) => {
            if (result.isCancelled) {
              alert('Login cancelled');
            } else {
              // Create response callback.
              const responseInfoCallback = (error, result) =>{
                if (error) {
                  console.log(error);
                  alert('Error fetching data: ' + error.toString());
                } else {
                  this.setState({
                    fblogindone:true
                  });
                }
              }
              // Create a graph request asking for user email and names with a callback to handle the response.
              const infoRequest = new GraphRequest('/me',{
                  parameters: {
                    fields: {
                      string: 'id,name,email'
                    }
                  }
                },
                responseInfoCallback
              );
              // Start the graph request.
              new GraphRequestManager().addRequest(infoRequest).start()
            }
          },
          function(error) {
            alert('Login fail with error: ' + error);
          }
        );
    }