简体   繁体   English

如何修复错误'FB'在create-react-app项目中没有定义no-undef?

[英]How to fix error 'FB' is not defined no-undef on create-react-app project?

I made the project using this link as my starting file. 我使用此链接作为我的起始文件制作了项目。

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

But after i tried to make Facebook login button with follow by their official docs with this. 但在我试图制作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'));
}

So i got this errors when the browser was refreshed. 所以我在刷新浏览器时遇到了这个错误。

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)

So i guess because ESLint that cause this errors. 所以我猜因为ESLint导致了这个错误。 How can i fix this or make the exception for this FB variable? 我该如何解决这个问题或者为这个FB变量做例外?

Thanks! 谢谢!

ESLint doesn't know that the variable FB is a global. ESLint不知道变量FB是全局变量。 You can declare a variable you referenced as a global by adding the following to the top of your file: 您可以通过将以下内容添加到文件顶部来声明您引用为全局的变量:

/*global FB*/

For more information, check out the "Rule Details" section on the official ESLint docs: http://eslint.org/docs/rules/no-undef 有关更多信息,请查看官方ESLint文档中的“规则详细信息”部分: http ://eslint.org/docs/rules/no-undef

If you use Create React App, you need to explicitly grab global variables from window . 如果使用Create React App,则需要从window显式获取全局变量。
For example: 例如:

// It's a global so need to read it from window
const FB = window.FB;

However, if the library is available as an npm package, you can use imports: 但是,如果库可用作npm包,则可以使用导入:

// It's an npm package so you can import it
import $ from 'jquery';

I hope this helps! 我希望这有帮助!

Based on what @dan-abramov stated in a comment to another answer: 基于@ dan-abramov对另一个答案的评论中所说的:

Don't use FB , use window.FB 不要使用FB ,请使用window.FB

This way, you define explicitely where the variable FB needs to come from. 这样,您可以明确定义变量FB需要来自何处。

Put this in your .eslintrc file, so you only have to define it once and not in every single file: 把它放在.eslintrc文件中,所以你只需要定义一次而不是每个文件:

"globals": {
    "FB": true
}

我可以通过使用this.FB而不是FB来解决这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM