Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing "'React' is declared but its value is never read" error when using tsconfig.json jsx setting with react-jsx value #41882

Closed
vkrol opened this issue Dec 8, 2020 · 18 comments · Fixed by #41905
Assignees
Labels
Bug A bug in TypeScript Domain: JSX/TSX Relates to the JSX parser and emitter Fix Available A PR has been opened for this issue

Comments

@vkrol
Copy link

vkrol commented Dec 8, 2020

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

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Dec 8, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Dec 8, 2020
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Dec 9, 2020
@weswigham weswigham added Bug A bug in TypeScript Domain: JSX/TSX Relates to the JSX parser and emitter and removed Needs Investigation This issue needs a team member to investigate its status. labels Dec 9, 2020
@ptorsson
Copy link

ptorsson commented Dec 24, 2020

Having the same problem in version 4.1.3.

error TS6133: 'React' is declared but its value is never read.
import React from "react"

export const TestComp = () => {
  return (
    <div>test</div>
  )
}

@doylemark
Copy link

In React 17 you no longer need to import react when writing JSX

@chj-damon
Copy link

In React 17 you no longer need to import react when writing JSX

If I remove it. another error will occur.

'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.ts(2686)

@weswigham
Copy link
Member

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.

@jethrolarson
Copy link

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/

@AdrienLemaire
Copy link

In React 17 you no longer need to import react when writing JSX

If I remove it. another error will occur.

'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.ts(2686)

I'm still getting TS6133: 'React' is declared but its value is never read errors 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 #41905 (comment).
Checking the git logs of v4.2.2, I can find the related commit 675cd4d

@gligoran
Copy link

gligoran commented May 4, 2021

In React 17 you no longer need to import react when writing JSX

We still use React 16. How can we get rid of this error?

@ripter
Copy link

ripter commented May 4, 2021

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.

bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue May 18, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue May 18, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue May 18, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue May 20, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue May 21, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue May 23, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
@FOG5490
Copy link

FOG5490 commented May 27, 2021

Remove import React in React 17 cause this error:
ESLint: 'React' must be in scope when using JSX(react/react-in-jsx-scope)

@vkrol
Copy link
Author

vkrol commented May 27, 2021

@FOG5490 You need to disable this rule https://ru.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#eslint.

@FOG5490
Copy link

FOG5490 commented May 27, 2021

@vkrol Thank you, I was about to update my comment also. I just found out that comment.

bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue May 29, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue Jun 11, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue Sep 7, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue Sep 18, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
bobwhitelock added a commit to bobwhitelock/todotxt-ui that referenced this issue Sep 18, 2021
In React 17 this is no longer necessary just to use JSX (see
microsoft/TypeScript#41882 (comment)).
@gligoran
Copy link

For those of you that use setups that don't support the new JSX Transform, check your tsconfig.json. The jsx compiler option needs to be set to "react", not "react-jsx" as I had:

{
  "compilerOptions": {
    "jsx": "react",
  },
}

Here's the info on all the available settings for this option and what they mean: https://www.typescriptlang.org/docs/handbook/jsx.html

@idelice
Copy link

idelice commented Oct 18, 2021

@sirmong
Copy link

sirmong commented Jul 13, 2022

For those of you that use setups that don't support the new JSX Transform, check your tsconfig.json. The jsx compiler option needs to be set to "react", not "react-jsx" as I had:

{
  "compilerOptions": {
    "jsx": "react",
  },
}

Here's the info on all the available settings for this option and what they mean: https://www.typescriptlang.org/docs/handbook/jsx.html

Thank you! That helped a lot!

kodiakhq bot pushed a commit to recipeyak/recipeyak that referenced this issue Aug 1, 2022
Basically:
- Replace web pack setup with vite and upgrade various dependencies and cull unnecessary ones.
- Also remove TSLint. Will replace with typescript-eslint rules eventually.
- Got rid of the hacky landing page static generation.

Various road bumps along the way:

- https://javascript.plainenglish.io/how-to-set-up-path-resolving-in-vite-ad284e0d9eae
- fix sass imports vitejs/vite#5764 (comment)
- Then needed to rewrite the alias for typescript again to match the types
- Replace `process`. With the `import.meta.env` thing
- https://stackoverflow.com/questions/64677212/how-to-configure-proxy-in-vite
- Fix imports for static files from `requires()`
- Had to fix proxying for `/avatar` and `/api` in dev
- Ran into remarkjs/react-markdown#339 (comment)
    - Upgrade markdown react to fix
- Migrate from `react-helmet` to fix some deprecation warnings
- Vite has a different jsx config, so no need to import React
    - microsoft/TypeScript#41882
- Vitest issue:
    - https://github.com/vitest-dev/vitest/blob/57c2367196b3fd978a04fa38ebdac7a5b6ef9b16/packages/vite-node/src/client.ts#L178-L179
- Couldn’t import react-dnd, upgraded
    - react-dnd/react-dnd#3368
- `import { isUndefined } from "util"` didn’t work
- Favicon via https://github.com/darkobits/vite-plugin-favicons to replace web pack equivalent 
- Issue with React router browser history in vitest, it exploded until jsdom was setup
- Question: https://stackoverflow.com/questions/71703933/what-is-the-difference-between-vite-and-vite-preview
- Vitest vscode integration broken :/
    - vitest-dev/vscode#44
- Tried happy-dom but it doesn’t work, lots of issues, supposed to be faster
- Took a while to get MSW in a good place, had to write some stuff so missing endpoint mocks fail the test
    - I think there's room for improvement here in terms of developer experience
    - Test with react testing library and API calls
        - https://www.npmjs.com/package/nock
            - Doesn’t fail test on unknown mock
            - https://stackoverflow.com/questions/69430232/jest-nock-how-to-fail-a-test-if-a-non-mocked-request-is-made
        - MSW
            - Doesn’t fail test on unknown mock
            - mswjs/msw#946
        - Relay’s mockEnvironment
          - couldn't easily find thorough examples
        - Apollo mock provider
            - Doesn’t fail test on unknown mock
- Added a visualize plugin similar to a web pack analyzer thing, but it’s slightly off with the numbers it gives for sizes:
    - btd/rollup-plugin-visualizer#96
- TODO:
    - ESLINT rules, replace what tslint provided, eslint-cake anyone?
        - https://typescript-eslint.io/rules/no-floating-promises/
        - And more!!!
    - Replace lodash with lodash-es
        - Or maybe: https://github.com/onebay/vite-plugin-imp
        - Although lodash-es seems cleaner
    - SSR  for landing page?
@hariacharya80
Copy link

hariacharya80 commented Jun 19, 2023

In React 17 you no longer need to import react when writing JSX

is it same for the v18?

@f1am3d
Copy link

f1am3d commented Jun 30, 2023

@doylemark , I am on version 18 and I have this error, lol

@hariacharya80
Copy link

@doylemark , I am on version 18 and I have this error, lol

Am also facing the same issue

@ygqygq2
Copy link

ygqygq2 commented Oct 14, 2023

eslint rule:
'react/jsx-uses-react': 'off',
'react/react-in-jsx-scope': 'off',

GKerfImf added a commit to GKerfImf/DePuzzle that referenced this issue Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: JSX/TSX Relates to the JSX parser and emitter Fix Available A PR has been opened for this issue
Projects
None yet