简体   繁体   English

如何修复 eslint 在反应中的“组件已定义但从未使用”?

[英]How to fix "Component is defined but never used" for eslint in react?

This is the config i have for eslint in .eslintrc.json file:这是我在.eslintrc.json文件中为 eslint 设置的配置:

 {
        "env": {
            "browser": true,
            "commonjs": true,
            "es6": true
        },
        "extends": "eslint:recommended",
        "parserOptions": {
            "ecmaVersion": 6,
            "ecmaFeatures": {
                "experimentalObjectRestSpread": true,
                "jsx": true
            },
            "sourceType": "module"
        },
        "plugins": [
            "react"
        ],
        "rules": {
            "react/jsx-uses-react": "error",
            "react/jsx-uses-vars": ["error"],
            "indent": 0,
            "linebreak-style": [
                "error",
                "windows"
            ],
            "quotes": [
                "error",
                "single"
            ],
            "semi": [
                "error",
                "always"
            ]
        },
        "settings": {
        "react": {
          "pragma": "React",
          "version": "15.0"
        }
      }
    }

This is the app.jsx from the react workspace below:这是下面反应工作区中的app.jsx

 import React, { Component } from 'react';

    class App extends React.Component {
      render () {
        return(
          <h1>Hello Newbies</h1>
        );
      }
    }

    export default App;

I am getting error from eslint 'Component' is defined but never used.我从 eslint 收到错误'Component' is defined but never used. I can not fix this by doing anything so far but digging the web.到目前为止,我无法通过挖掘 web 来解决此问题。 How can i fix this error?我该如何解决这个错误? Help would be very much appreciated.帮助将不胜感激。

尝试

class App extends Component {

Option 1: You don't need to import {Component} since you extends React.component and React is already imported. 选项1:您不需要导入{Component},因为您扩展了React.component且React已经被导入。

Option 2: You can extend your App with Component instead of React.Component 选项2:您可以使用Component而不是React.Component扩展您的App

Just remove "import React, { Component } from 'react';"只需删除“import React, { Component } from 'react';” this line and warning will be resolved as this will be not used in functional based Components.此行和警告将得到解决,因为这不会在基于功能的组件中使用。

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

相关问题 React中的无状态组件和“Props已定义但从未使用过”ESLint错误 - Stateless component in React and “Props is defined but never used” ESLint error ESLint 警告事件已定义但从未使用过 - ESLint warning event is defined but never used &#39;React&#39; 已定义但从未使用 - 'React' is defined but never used 仅在一个文件中使用 ESLint “require is not defined”时如何解决? - How to fix ESLint “require is not defined” when it is used in just one file? [eslint] src\App.js 第 2:8 行中的 React js 错误警告:'person' 已定义但从未使用过 - React js error WARNING in [eslint] src\App.js Line 2:8: 'person' is defined but never used 如何修复我的反应应用程序中的 eslint 错误“未定义 pendo” - how to fix eslint error 'pendo is not defined' in my react application “&#39;React&#39; 在定义之前就被使用了。” eslint 警告 - “'React' was used before it was defined.” eslint warning Eslint with Vue 给出 function 已定义但从未使用过 no-unused-vars - Eslint with Vue gives function is defined but never used no-unused-vars eslint:禁用警告-特定 function 的“已定义但从未使用”? - eslint: disable warning - `defined but never used` for specific function? @typescript-eslint/eslint-plugin 错误:定义了“路由”但从未使用过(no-unused-vars) - @typescript-eslint/eslint-plugin error: 'Route' is defined but never used (no-unused-vars)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM