reactjs - 如何解决 setTimeout 的 no-undef 错误

标签 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)

如何解决此警告?

最佳答案

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

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

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

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

关于reactjs - 如何解决 setTimeout 的 no-undef 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52472800/

相关文章:

webpack - 如何让 eslint 导入/首先理解我的 webpack 别名?

eslint-plugin-testing-library 没有捕获 lint 错误

reactjs - 延迟建议时间 Material-UI Autocomplete 组件

css - ReactTable 组件中的固定列

node.js - Mongoose Node - 请求对多个文档进行多次更新,全部成功还是全部取消?

javascript - 全局变量存储在本地后如何访问它?

reactjs - 在 eslintrc.json 中关闭 eslint 规则

reactjs - eslint + jsconfig + nextjs 模块路径别名(绝对路径导入 - @)

javascript - 如果 div 为空,Eslint airbnb 会强制执行自关闭 div 标签。如何禁用此规则?

jquery - react-materialize 侧边栏问题 TypeError : $(. ..).sideNav 不是函数