如何解决setTimeout的no-undef错误

时间:2018-09-24 04:41:42

标签: reactjs eslintrc

我的代码

  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 个答案:

答案 0 :(得分:4)

问题是您的.eslintrc中的environment未配置。

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

您可以将其配置为浏览器 [您的React / Redux / JavaScript代码]和节点 [Webpack并构建相关代码]。

"env": {
  "browser": true,
  "node": true
},