TypeScript: Missing "'React' is declared but its value is never read" error when using tsconfig.json jsx setting with react-jsx value
TypeScript Version: 4.2.0-dev.20201208
Search Terms: React, JSX transform, noUnusedLocals, missing error.
Code
import React from "react";
function Bar() {
return <div />;
}
export function Foo() {
return <Bar />;
}
Expected behavior:
index.tsx:1:1 - error TS6133: 'React' is declared but its value is never read.
Actual behavior: No error.
Additional info: The compiler throws error correctly if JSX does not use custom components. For example:
import React from "react";
export function Foo() {
return <div />;
}
Playground Link: N/A (Playground doesn’t support "jsx": "react-jsx" tsconfig.json setting.
Related Issues: N/A
Minimal reproducible example: https://github.com/vkrol/typescript-missing-nounusedlocals-error-for-react-with-new-jsx-transform
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 26
- Comments: 18 (1 by maintainers)
Commits related to this issue
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- Remove unnecessary imports of React In React 17 this is no longer necessary just to use JSX (see https://github.com/microsoft/TypeScript/issues/41882#issuecomment-754099534). — committed to bobwhitelock/todotxt-ui by bobwhitelock 3 years ago
- ref(ui): use vite instead of webpack (#739) Basically: - Replace web pack setup with vite and upgrade various dependencies and cull unnecessary ones. - Also remove TSLint. Will replace with typescr... — committed to recipeyak/recipeyak by sbdchd 2 years ago
- fix: React is declared but its value is never read also see: https://github.com/microsoft/TypeScript/issues/41882 — committed to GKerfImf/DePuzzle by GKerfImf 6 months ago
In React 17 you no longer need to import react when writing JSX
For those of you that use setups that don’t support the new JSX Transform, check your tsconfig.json. The
jsxcompiler option needs to be set to"react", not"react-jsx"as I had:Here’s the info on all the available settings for this option and what they mean: https://www.typescriptlang.org/docs/handbook/jsx.html
Using React 17, if I take out the import I get this error instead: ‘React’ must be in scope when using JSX" So it doesn’t seam optional.
If I remove it. another error will occur.
Having the same problem in version 4.1.3.
Remove
import Reactin React 17 cause this error:ESLint: 'React' must be in scope when using JSX(react/react-in-jsx-scope)@FOG5490 You need to disable this rule https://ru.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#eslint.
For others that may stumble across this: If you need to use react16 with react-scripts 4 and typescript 4.1+ you may need to enable this env variable: DISABLE_NEW_JSX_TRANSFORM so that react-scripts doesn’t keep overwriting the tsconfig with the incompatible value. More deets: https://create-react-app.dev/docs/advanced-configuration/
Thank you! That helped a lot!
We still use React 16. How can we get rid of this error?
I’m still getting
TS6133: 'React' is declared but its value is never readerrors on TS 4.2.2, and if trying to remove the imports, getting the UMD global error as well. The fix should have been merged by 4.2.0 https://github.com/microsoft/TypeScript/pull/41905#issuecomment-752204827. Checking the git logs of v4.2.2, I can find the related commit 675cd4d7ce175dbd3875b7aaedacfc496435b816eslint rule: ‘react/jsx-uses-react’: ‘off’, ‘react/react-in-jsx-scope’: ‘off’,
@doylemark , I am on version 18 and I have this error, lol
is it same for the v18?
@vkrol Thank you, I was about to update my comment also. I just found out that comment.
You only don’t need to import it for the tag factory; if you actually use it for other things (eg, types, base classes, hooks) you’ll still need to explicitly import it, like anything else.