VSCode 在保存时从导入中删除 React

MAK*_*MAK 15 eslint visual-studio-code vscode-extensions

切换到 vscode 1.68.1 后

每次保存文件时,它都会首先自动从导入中删除 React,虽然这可能是 eslint 问题,因为我使用 eslint 进行格式化,但删除 eslint 插件后问题仍然相同

这是我的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"
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 .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/"]
          }
        }
      }
};
Run Code Online (Sandbox Code Playgroud)

Mud*_*Ali 12

尝试在eslintrc中添加这些规则:

{
  "rules": {
    "react/jsx-uses-react": 0,
    "react/react-in-jsx-scope": 0
  }
}
Run Code Online (Sandbox Code Playgroud)

或者

尝试将"jsx": "react"添加到您的jsconfig中:

{
  "compilerOptions": {
    "baseUrl": ".",
    "jsx": "react",
    "paths": {
      "*": ["*", "src/*"]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这有助于 VS-Code 了解React是由文件中的 jsx 元素隐式引用的。

我在多个项目中遇到了同样的问题,添加这些规则后,为我解决了问题。


tHe*_*SiD 0

{
  // ...
  "rules": {
    // ...
    "react/jsx-uses-react": "on",
    "react/react-in-jsx-scope": "on"
  }
}
Run Code Online (Sandbox Code Playgroud)

将这些规则添加到您的 eslint 配置中