ESLint reports no-unused-vars for React components and imports that are used
ESLint with React gives no-unused-vars errorsShort answer
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused. Enable eslint-plugin-react's jsx-uses-vars (part of its recommended config). In TypeScript projects use the typescript-eslint version of the rule instead.
Fix it fast
1Find out which cause you have
Run ESLint on one file and read the rule id at the end of each message. It tells you which rule produced the report.
npx eslint src/App.jsx2Match what you see, then jump to its fix
- 1
A component used only as <Component /> is reported as unusedJSX usage is not marked as useFix - 2
Type-only imports, enums or generics are reported in .ts or .tsx filesTypeScript files are checked by the base ruleFix - 3
The default React import is reported as unused in files that only contain JSXAn import of React is no longer neededFix
- 1
What does the error look like?
ESLint with React gives no-unused-vars errorsno-unused-vars reported for a component that is rendered as a JSX tag
Cause 1 of 3
JSX usage is not marked as use
1Check
Core no-unused-vars does not detect variables used in JSX. The rule jsx-uses-vars from eslint-plugin-react marks them as used, and it is switched on by the plugin's recommended config.
npm ls eslint-plugin-react2Fix
Install the plugin and extend its recommended config, then run ESLint again.
npm install --save-dev eslint-plugin-react
// .eslintrc.cjs
module.exports = {
extends: ['eslint:recommended', 'plugin:react/recommended'],
};3Verify
The reports for components used in JSX are gone.
npx eslint srcConfirmed by github.com
Cause 2 of 3
TypeScript files are checked by the base rule
1Check
typescript-eslint says the base rule can report incorrect errors on TypeScript code and must be disabled in favour of its own extension rule.
grep -n "no-unused-vars" eslint.config.* .eslintrc* 2>/dev/null2Fix
Turn off the base rule and turn on the typescript-eslint one.
export default [
{
rules: {
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
},
},
];3Verify
Run ESLint on a .tsx file.
npx eslint srcConfirmed by typescript-eslint.io
Cause 3 of 3
An import of React is no longer needed
1Check
With the new JSX transform you do not need to import React just to use JSX. The React team also notes that react/jsx-uses-react and react/react-in-jsx-scope are then no longer necessary.
grep -rln "import React from 'react'" src/2Fix
Remove the default import and keep named imports for hooks.
// before
import React, { useState } from 'react';
// after
import { useState } from 'react';3Verify
ESLint and the build both pass.
npx eslint src && npm run buildConfirmed by legacy.reactjs.org
How do I stop it coming back?
- Keep eslint-plugin-react in the project when it is a React app.
- Use the typescript-eslint rule for all TypeScript sources.
- Prefix intentionally unused arguments with an underscore and set argsIgnorePattern to ^_.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
TypeScript rejects MUI Data Grid columns that set a type
A TypeScript error inside a column's renderCell callback
A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.
2 causes, each with a fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fixWeb apps
Upgrading to Next.js 16: middleware becomes proxy, and the other things that break
Missing Required default.js for Parallel Route
Next.js 16 renames middleware.ts to proxy.ts and its exported function to proxy.
6 causes, each with a fix