【问题标题】:react.js 'x' is assigned a value but never used no-unused-varsreact.js 'x' 被分配了一个值,但从未使用过 no-unused-vars
【发布时间】:2020-05-04 03:54:46
【问题描述】:

我已经设置了 eslint 和 eslint-plugin-react。

当我运行 ESLint 时,linter 返回 no-unused-vars 。

我假设它没有识别出我正在使用 JSX 或 React 语法。有什么想法吗?

第 5:11 行:“x”被赋值但从未使用过 no-unused-vars

例子:

请帮帮我


文件组件内部

import React,{ Component } from 'react';
class Item extends Component{
   render () {
       const x = 1;
       return (
         <div>test</div>
       );
    }
};

export default Item;

在文件 .eslintrc.json 中

{
"env": {
    "browser": true,
    "es6": true
},
"extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "react-app","prettier"
],
"settings": {
  "react": {
    "createClass": "createReactClass"
    "pragma": "React",
    "version": "detect",
    "flowVersion": "0.53"
  },
  "propWrapperFunctions": [
      "forbidExtraProps",
      {"property": "freeze", "object": "Object"},
      {"property": "myFavoriteWrapper"}
  ],
  "linkComponents": [
    "Hyperlink",
    {"name": "Link", "linkAttribute": "to"}
  ]
},
"parserOptions": {
    "ecmaVersion": 2018,
    "ecmaFeatures": {
      "jsx": true
    }
},
"plugins": [
    "react","prettier"
],
"rules": {
  "react/jsx-uses-react": "error",
  "react/jsx-uses-vars": "error",
  "no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }],
}

}

【问题讨论】:

  • const x = 1; 并且你从不使用x。您认为为什么会使用该作业?
  • 发生这种情况是因为您定义了 x 但没有使用它。所以,只需删除它
  • 欢迎来到 SO!请注意,尽可能保持提供的代码尽可能明确,但尽可能简短。您可能想看看stackoverflow.com/help/minimal-reproducible-example 以获得一些灵感。

标签: javascript reactjs function eslint eslintrc


【解决方案1】:

ESLint lint 行为是正确的。你已经声明了x,但不要在你的 JSX 中使用。 如果使用它应该会消失:)

import React,{ Component } from 'react';
class Item extends Component{
   render () {
       const x = 1;
       return (
         <div>test {x}</div>
       );
    }
};

export default Item;

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    • 2020-04-21
    • 2017-03-16
    • 1970-01-01
    相关资源
    最近更新 更多