ReactJS和Facebook API:获取错误“FB未定义('FB' is not defined)”。

5

我完全是ReactJS的新手,只是想玩一下并测试一些东西。我在研究如何实现Facebook API时,却遇到了这个错误信息:

error  'FB' is not defined  no-undef

我不确定如何解决这个问题。希望能够得到帮助,看看我在这里做错了什么。我是否需要导入某种Facebook API?

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {

  componentDidMount() {
    window.fbAsyncInit = function() {
      FB.init({
        appId      : '123141151155', //random app number
        cookie     : true,  // enable cookies to allow the server to access
                          // the session
        xfbml      : true,  // parse social plugins on this page
        version    : 'v2.3'
      });
      //JS SDK initialized, now you can use it
      FB.XFBML.parse();

      // Now that we've initialized the JavaScript SDK, we call
      // FB.getLoginStatus().  This function gets the state of the
      // person visiting this page and can return one of three states to
      // the callback you provide.  They can be:
      //
      // 1. Logged into your app ('connected')
      // 2. Logged into Facebook, but not your app ('not_authorized')
      // 3. Not logged into Facebook and can't tell if they are logged into
      //    your app or not.
      //
      // These three cases are handled in the callback function.
      FB.getLoginStatus(function(response) {
        this.statusChangeCallback(response);
      }.bind(this));
    }.bind(this);

    // Load the SDK asynchronously
    (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 = "//connect.facebook.net/en_US/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
  }

  // Here we run a very simple test of the Graph API after login is
  // successful.  See statusChangeCallback() for when this call is made.
  testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
    console.log('Successful login for: ' + response.name);
    document.getElementById('status').innerHTML =
      'Thanks for logging in, ' + response.name + '!';
    });
  }

  // This is called with the results from from FB.getLoginStatus().
  statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
      // Logged into your app and Facebook.
      this.testAPI();
    } else if (response.status === 'not_authorized') {
      // The person is logged into Facebook, but not your app.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    } else {
      // The person is not logged into Facebook, so we're not sure if
      // they are logged into this app or not.
      document.getElementById('status').innerHTML = 'Please log ' +
      'into Facebook.';
    }
  }

  // This function is called when someone finishes with the Login
  // Button.  See the onlogin handler attached to it in the sample
  // code below.
  checkLoginState() {
    FB.getLoginStatus(function(response) {
      this.statusChangeCallback(response);
    }.bind(this));
  }

  handleClick() {
    FB.login(this.checkLoginState());
  }

  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Welcome to React</h2>
        </div>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;


如果FB在全球范围内可用吗?否则它需要在此模块中被导入/定义。 - lux
@FacundoLaRocca 我觉得那就是我缺失的东西,因为我刚接触reactjs不久,所以不是很确定,但我觉得我开始理解它的工作原理了。 - hellomello
我几乎可以确定你需要导入这个库,我正在阅读官方文档,我认为这是你缺少的。所有其他代码似乎都没问题。如果添加了导入后仍然有问题,请告诉我。 - Facundo La Rocca
@FacundoLaRocca 导入后没有出现错误,谢谢! - hellomello
我会将评论发布为答案,这样对其他人可能会有帮助。您是否介意接受它并投票支持?谢谢。 - Facundo La Rocca
显示剩余4条评论
2个回答

13

我也遇到了相同的问题,并想出了另一种解决方案。

'FB' is not defined no-undef 可以通过在引用时从 window 中获取来解决,即使用 window.FB 而不是仅使用 FB。这种解决方法无需安装任何其他的 npm 包。

这里有一个可以将该组件集成到 React 项目中的可工作的repo,在 src/App.js 中,您将找到与上述代码片段相同的内容,并且确切地在 Stack Overflow 的问题中: Implement Facebook API login with reactjs.

提供这个替代方案作为额外选项,希望能够帮到您。


我正在使用Vue和webpack,这是唯一有效的方法来通过错误屏幕,非常感谢! - Camilo Delvasto

-3
根据文档,您似乎忘记导入/引用库了。我认为您只需要在开头导入该文件,例如:
import FB from 'fb';

希望它有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接