Facebook登录后发送(React)

时间:2018-03-13 17:58:24

标签: facebook reactjs api redux

我在React中很新,我正在尝试整合Facebook登录,在阅读了很多博客后我没有Facebook Api的问题,但我不能让它在我的应用程序中运行,我是收到此消息“Uncaught TypeError:无法读取未定义的属性'dispatch',我正在使用React / Redux,有什么建议吗?

componentDidMount(){
    window.fbAsyncInit = function() {
    window.FB.init({
      appId      : 'ID',
      cookie     : true,
      xfbml      : true,
      version    : 'v2.1'
    });

    window.FB.Event.subscribe('auth.statusChange', (response) => {
    if (response.authResponse){
        console.log('Welcome!  Fetching your information.... ');
        FB.api(
        '/me',
        'GET',
        {"fields":"id,name,email"},
        function(response) {
            this.props.dispatch(login(response.email, response.id, true),)
          }
        );
     } else{
        this.updateLoggedOutState()
     }   
    });
  }.bind(this);

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "https://connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
}

1 个答案:

答案 0 :(得分:0)

试试这个:

let dispatch = {this.props.dispatch};
window.FB.Event.subscribe('auth.statusChange', (response) => {
    if (response.authResponse){
        console.log('Welcome!  Fetching your information.... ');
        FB.api(
            '/me',
            'GET',
            {"fields":"id,name,email"},
            function(response) {
                dispatch(login(response.email, response.id, true),)
            }
        );
    } else{
        this.updateLoggedOutState()
    }   
});