Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode removing React from imports on save

after switching to vscode 1.68.1

every time on saving the file its automatically removing the React from the import at first i though this could be an eslint issue as i am using eslint for formatting but after removing eslint plugin issue remains the same

here is my settings.json

{
    "eslint.alwaysShowStatus": true,
    "editor.formatOnSave": true,
    "files.eol": "\r\n",
    "editor.codeActionsOnSave": {
        "source.fixAll": true,
        "source.organizeImports": true
    },
    "editor.defaultFormatter": "dbaeumer.vscode-eslint",
    "prettier.arrowParens": "avoid",
    "prettier.embeddedLanguageFormatting": "off",
    "prettier.enable": false,
    "eslint.format.enable": false,
   
    "[css]": {
        "editor.defaultFormatter": "aeschli.vscode-css-formatter"
    },
    "[html]": {
        "editor.defaultFormatter": "vscode.html-language-features"
    },
    "[json]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "[jsonc]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    }
}

and here is the .eslintrc

module.exports = {
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": "airbnb",
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "plugins": [
        "react", "react-hooks"
    ],
    "rules": {
        "linebreak-style": ["error", "windows"],
        "react/forbid-prop-types": 0,
        "react/prop-types": 0,
        "max-len": ["error", { "code": 220 }]
    },
    "settings": {
        "import/resolver": {
          "node": {
            "moduleDirectory": ["node_modules", "src/"]
          }
        }
      }
};
like image 302
MAK Avatar asked Nov 30 '25 06:11

MAK


2 Answers

Try adding these rules in eslintrc:

{
  "rules": {
    "react/jsx-uses-react": 0,
    "react/react-in-jsx-scope": 0
  }
}

OR

Try adding "jsx": "react" to your jsconfig:

{
  "compilerOptions": {
    "baseUrl": ".",
    "jsx": "react",
    "paths": {
      "*": ["*", "src/*"]
    }
  }
}

This helps VS-Code understand that React is implicitly referenced by the jsx elements in your file.

I had the same problem in multiple projects, after adding these rules, solves the problem for me.

like image 178
Mudassar Avatar answered Dec 02 '25 04:12

Mudassar


add type on your tsx file, to prevent removed automatically by vscode

import React from 'react';
type _react = typeof React

.eslint

{
  rules: {
    'react/jsx-uses-react': 0, // ignore validation must include react scope in jsx/tsx
    'react/react-in-jsx-scope': 0, // ignore validation must include react scope in jsx/tsx
    'no-unused-vars': 'off', // disable default eslint unused variable
    '@typescript-eslint/no-unused-vars': [ // ignore validation variable with prefix _
      'warn',
      {
        argsIgnorePattern: '^_',
        varsIgnorePattern: '^_',
        caughtErrorsIgnorePattern: '^_'
      }
    ]
  }
}
like image 44
Dimas Lanjaka Avatar answered Dec 02 '25 05:12

Dimas Lanjaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!