代码之家  ›  专栏  ›  技术社区  ›  Kim Stacks

如何解决setTimeout的no undef错误

  •  2
  • Kim Stacks  · 技术社区  · 7 年前

    我的代码

      componentDidMount() {
        // we add a hidden class to the card and after 700 ms we delete it and the transition appears
        this.timeOutFunction = setTimeout(
          function () {
            this.setState({cardAnimaton: ""});
          }.bind(this),
          700
        );
      }
    
      componentWillUnmount() {
        clearTimeout(this.timeOutFunction);
        this.timeOutFunction = null;
      }
    
      componentWillMount() {
        if (this.state.logged_in) {
          this.props.history.push("/dashboard");
        }
      }
    

    .eslintrc

    {
        "parser": "babel-eslint",
        "plugins": [
            "react"
        ],
        "extends": [
            "eslint:recommended",
            "plugin:react/recommended"
        ],
        "rules": {
           "no-set-state": "off",
           "react/no-multi-comp":  [1, { "ignoreStateless": true }]
        },
    
        "parserOptions": {
          "ecmaVersion": 6,
          "sourceType": "module",
          "ecmaFeatures": {
              "jsx": true,
              "modules": true
          }
      },
      "globals": {
        "localStorage": true,
        "fetch": true
    },
      "settings": {
        "react": {
            "pragma": "React",
            "version": "16.4.1"
        }
        }
    }
    

    我得到以下警告

    setTimeout is not defined (no-undef)
    clearTimeout is not defined (no-undef)
    

    1 回复  |  直到 7 年前
        1
  •  13
  •   aravind_reddy    7 年前

    问题是你的 environment 在里面 .eslintrc

    每个环境都会带来一组预定义的全局变量。

    [您的React/Redux/JavaScript代码]& 节点

    "env": {
      "browser": true,
      "node": true
    },
    
    推荐文章