Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Visual Studio Code using "react-jsx" as jsx value with create-react-app

I'm struggling with following "error" in VSCode :

Argument for '--jsx' option must be: 'preserve', 'react-native', 'react' 

Hence, react-scripts (create-react-app) automatically sets the jsx key to react-jsx value, when react value seems to work.

Actually, the code works perfectly and displays the page I want, but the IDE is underlining kinda everything as errors, saying :

Cannot use JSX unless the '--jsx' flag is provided. 

Here is my tsconfig.json :

{   "compilerOptions": {     "target": "es5",     "lib": [       "dom",       "dom.iterable",       "esnext"     ],     "allowJs": true,     "skipLibCheck": true,     "esModuleInterop": true,     "allowSyntheticDefaultImports": true,     "strict": true,     "forceConsistentCasingInFileNames": true,     "noFallthroughCasesInSwitch": true,     "module": "esnext",     "moduleResolution": "node",     "resolveJsonModule": true,     "isolatedModules": true,     "noEmit": true,     "jsx": "react-jsx"   },   "include": [     "src"   ] }  

, and my package.json (default one provided by create-react-app + packages updated) :

{   "name": "front-office",   "version": "0.1.0",   "private": true,   "dependencies": {     "@testing-library/jest-dom": "^5.11.6",     "@testing-library/react": "^11.2.2",     "@testing-library/user-event": "^12.2.2",     "@types/jest": "^26.0.15",     "@types/node": "^14.14.9",     "@types/react": "^17.0.0",     "@types/react-dom": "^17.0.0",     "bootstrap": "^4.5.3",     "react": "^17.0.1",     "react-bootstrap": "^1.4.0",     "react-dom": "^17.0.1",     "react-scripts": "4.0.1",     "typescript": "^4.1.2",     "web-vitals": "^1.0.1"   },   "scripts": {     "start": "react-scripts start",     "build": "react-scripts build",     "test": "react-scripts test",     "eject": "react-scripts eject"   },   "eslintConfig": {     "extends": [       "react-app",       "react-app/jest"     ]   },   "browserslist": {     "production": [       ">0.2%",       "not dead",       "not op_mini all"     ],     "development": [       "last 1 chrome version",       "last 1 firefox version",       "last 1 safari version"     ]   } }  

I'm using the latest version of Typescript, and I updated all my packages with ncu, closed/opened VSCode (works sometimes with tsconfig !) and nothing seems to resolve that.

I'm pretty sure it's a VSCode problem, but I'm running out of ideas about how to resolve that.

Any of you guys got ideas ?

Edit:

If you followed the above steps and VSCode still shows the "-jsx" error, make sure you have disabled the 'TypeScript God' extension (and any other TS extension until the issue no longer appears).

like image 553
Denis Lebon Avatar asked Nov 23 '20 18:11

Denis Lebon


People also ask

How do you fix Cannot use JSX unless the '-- JSX flag is provided?

To solve the error "Cannot use jsx unless the '--jsx' flag is provided", restart your IDE and development server and make sure your IDE is using the same version of TypeScript that your project uses. The project's TypeScript version and your IDE's TypeScript versions should match.

Does create React app use JSX?

JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React. createElement(component, props, ...children) .

Do I need to import React to use JSX?

All the JSX conversion is handled by React without you having to import or add anything. This new JSX transform is also supported in older versions of React so you can start using them even if you don't use React 17.

What is the difference between JSX and React?

JSX is just a syntax extension of JavaScript which allows users to write React components; React is an open source frontend JavaScript library for building complex UIs; it allows the programmers to create complex UIs from smaller components.


2 Answers

This is because VSCode's Typescript version is not using the newest babel features that create-react-app uses by default.

You can change VS Code to use the workspace's version of Typescript instead, which will fix this issue.

Open a TypeScript or JavaScript file and click on the TypeScript version number in the Status Bar. A message box will appear asking you which version of TypeScript VS Code should use

Select "Use Workspace Version" to use the newer Create React App typescript version.

like image 76
marksfrancis Avatar answered Sep 22 '22 04:09

marksfrancis


In PhpStorm (and the same is probably true for WebStorm) I got it to not complain about "react-jsx" as the value by ticking "Always download the most recent version of schemas" in Remote JSON Schemas.

The option that needs to be ticked

like image 26
Attila Szeremi Avatar answered Sep 21 '22 04:09

Attila Szeremi