最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - ESLint unused variable (React) - Stack Overflow

programmeradmin1浏览0评论

Why is ESLint constantly complaining about unused react prop, this code doesn't have any issue. Works as expected. The "Icon" prop is used withing the code.

const PageLink = ({ icon: Icon, title, ...props }) => (
  <Link
    {...props}
    className={cn(
      "p-2 rounded-xl bg-neutral-100 dark:bg-neutral-800 text-center",
      "flex gap-2 justify-center items-center"
    )}
  >
    <Icon className="size-4" />
    {title}
  </Link>
);

Below is the ESLint config file, it was generated by Vite. Not changed at all, just using the default.

import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'

export default [
  { ignores: ['dist'] },
  {
    files: ['**/*.{js,jsx}'],
    languageOptions: {
      ecmaVersion: 2020,
      globals: globals.browser,
      parserOptions: {
        ecmaVersion: 'latest',
        ecmaFeatures: { jsx: true },
        sourceType: 'module',
      },
    },
    plugins: {
      'react-hooks': reactHooks,
      'react-refresh': reactRefresh,
    },
    rules: {
      ...js.configs.recommended.rules,
      ...reactHooks.configs.recommended.rules,
      'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
      'react-refresh/only-export-components': [
        'warn',
        { allowConstantExport: true },
      ],
    },
  },
]

Why is ESLint constantly complaining about unused react prop, this code doesn't have any issue. Works as expected. The "Icon" prop is used withing the code.

const PageLink = ({ icon: Icon, title, ...props }) => (
  <Link
    {...props}
    className={cn(
      "p-2 rounded-xl bg-neutral-100 dark:bg-neutral-800 text-center",
      "flex gap-2 justify-center items-center"
    )}
  >
    <Icon className="size-4" />
    {title}
  </Link>
);

Below is the ESLint config file, it was generated by Vite. Not changed at all, just using the default.

import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'

export default [
  { ignores: ['dist'] },
  {
    files: ['**/*.{js,jsx}'],
    languageOptions: {
      ecmaVersion: 2020,
      globals: globals.browser,
      parserOptions: {
        ecmaVersion: 'latest',
        ecmaFeatures: { jsx: true },
        sourceType: 'module',
      },
    },
    plugins: {
      'react-hooks': reactHooks,
      'react-refresh': reactRefresh,
    },
    rules: {
      ...js.configs.recommended.rules,
      ...reactHooks.configs.recommended.rules,
      'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
      'react-refresh/only-export-components': [
        'warn',
        { allowConstantExport: true },
      ],
    },
  },
]

Share Improve this question edited Mar 14 at 7:44 Sadiq Salau asked Mar 14 at 5:48 Sadiq SalauSadiq Salau 1296 bronze badges 2
  • 1 You must have a eslint config file, where you would have configured an error for this. Search for something like this no-unused-vars or please share the file here – Tushar Shahi Commented Mar 14 at 6:06
  • @TusharShahi i have updated it – Sadiq Salau Commented Mar 14 at 7:45
Add a comment  | 

1 Answer 1

Reset to default 0

Given your eslint file, you have the rule:

'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],

which means throw an error when there are unused variables.

Just remove that key from the object and you should be good.

Rule def

发布评论

评论列表(0)

  1. 暂无评论