VSCODE从保存上的进口中删除React React

发布于 2025-02-09 08:41:38 字数 1804 浏览 3 评论 0原文

切换到VSCODE 1.68.1后,

每次保存文件自动保存文件,都会自动从导入中删除REECT 首先,虽然这可能是一个ESLINT问题,因为我正在使用Eslint进行格式化,但是在删除Eslint插件之后,问题仍然

是我的设置

{
    "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"
    }
}

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/"]
          }
        }
      }
};

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/"]
          }
        }
      }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

狼亦尘 2025-02-16 08:41:39

VS代码期望每个项目都声明a tsconfig.jsona href =“ https://code.visualstudio.com/docs/languages/jsconfig” rel =“ nofollow noreferrer”> jsconfig.json 有效地意味着

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

,如果您对代码中的进口进行了反应,则将保留它们。

使用“ nofollow noreferrer”> _JSX 转换,环境类型需要为react-jsx

{
  "compilerOptions": {
    "jsx": "react-jsx"
  }
}

VS Code expects that every project declares a tsconfig.json or jsconfig.json and if none is provided, it defaults it to

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

This effectively means that if you have React imports in your code, they will be preserved.

To adopt the latest react 17+ standard with the _jsx transform, the environment type needs to be react-jsx.

{
  "compilerOptions": {
    "jsx": "react-jsx"
  }
}
初见 2025-02-16 08:41:39
{
  // ...
  "rules": {
    // ...
    "react/jsx-uses-react": "on",
    "react/react-in-jsx-scope": "on"
  }
}

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

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

Add these rules to your eslint config

∝单色的世界 2025-02-16 08:41:39

在我的情况下,解决方案是评论这一点:

"editor.codeActionsOnSave": {
    // "source.organizeImports": true,
},

In my case solution was to comment this out:

"editor.codeActionsOnSave": {
    // "source.organizeImports": true,
},
遗心遗梦遗幸福 2025-02-16 08:41:39

我也遇到了同样的问题,当我发现它是Vscode的扩展程序时,将其降级为1.67.2解决了

I also had the same problem, when removing the extensions I found it was vscode, downgrading to version 1.67.2 solved it

北城挽邺 2025-02-16 08:41:38

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

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

尝试将“ jsx”:“ react” 添加到您的 jsconfig

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

这有助于vs-code了解< strong> react 由文件中的JSX元素隐含地引用。

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

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.

み青杉依旧 2025-02-16 08:41:38

尝试删除“ source.organizeimports”:True从您的设置中行,或将其设置为false

我认为VS代码认为React导入是不必要的,因为它仅隐含地使用(如果您的编译器需要明确的导入才能正确捆绑包,则可能是一个问题)。

Try removing the "source.organizeImports": true line from your settings, or setting it to false.

I think VS Code thinks the React import is unnecessary because its only used implicitly (which can be a problem if your compiler requires an explicit import to bundle correctly).

十秒萌定你 2025-02-16 08:41:38

在TSX文件上添加类型,以防止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: '^_'
      }
    ]
  }
}

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: '^_'
      }
    ]
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文