问题:如何修复错误 'FB' is not defined no-undef on create-react-app 项目?

我使用这个链接作为我的起始文件制作了这个项目。

https://github.com/facebookincubator/create-react-app

但是在我尝试制作 Facebook 登录按钮之后,他们的官方文档将使用此按钮。

componentDidMount(){
    console.log('login mount');
    window.fbAsyncInit = function() {
        FB.init({
            appId      : '320866754951228',
            xfbml      : true,
            version    : 'v2.6'
        });

        FB.getLoginStatus(function(response) {
            //this.statusChangeCallback(response);
        });

    };

    (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'));
}

因此,刷新浏览器时出现此错误。

Failed to compile.

Error in ./src/components/user_profile/LoginForm.js

/Sites/full_stack_production/xxxxx
  70:13  error  'FB' is not defined  no-undef
  76:13  error  'FB' is not defined  no-undef

✖ 2 problems (2 errors, 0 warnings)

所以我猜是因为 ESLint 导致了这个错误。我该如何解决这个问题或让这个FB变量例外?

谢谢!

解答

ESLint 不知道变量FB是全局变量。您可以通过将以下内容添加到文件顶部来将引用的变量声明为全局变量:

/*global FB*/

有关更多信息,请查看官方 ESLint 文档中的“规则详细信息”部分:http://eslint.org/docs/rules/no-undef

Logo

React社区为您提供最前沿的新闻资讯和知识内容

更多推荐