VSCODE从保存上的进口中删除React React
切换到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
VS代码期望每个项目都声明a
tsconfig.json
a href =“ https://code.visualstudio.com/docs/languages/jsconfig” rel =“ nofollow noreferrer”>
jsconfig.json
有效地意味着,如果您对代码中的进口进行了反应,则将保留它们。
使用“ nofollow noreferrer”>
_JSX
转换,环境类型需要为react-jsx
。VS Code expects that every project declares a
tsconfig.json
orjsconfig.json
and if none is provided, it defaults it toThis 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 bereact-jsx
.将这些规则添加到您的ESLINT配置
Add these rules to your eslint config
在我的情况下,解决方案是评论这一点:
In my case solution was to comment this out:
我也遇到了同样的问题,当我发现它是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
尝试在 eslintrc 中添加这些规则:
或
尝试将“ jsx”:“ react” 添加到您的 jsconfig :
这有助于vs-code了解< strong> react 由文件中的JSX元素隐含地引用。
在添加这些规则后,我在多个项目中也遇到了同样的问题,为我解决了问题。
Try adding these rules in eslintrc:
OR
Try adding "jsx": "react" to your jsconfig:
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.
尝试删除
“ source.organizeimports”:True
从您的设置中行,或将其设置为false
。我认为VS代码认为
React
导入是不必要的,因为它仅隐含地使用(如果您的编译器需要明确的导入才能正确捆绑包,则可能是一个问题)。Try removing the
"source.organizeImports": true
line from your settings, or setting it tofalse
.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).在TSX文件上添加类型,以防止Vscode
.ESLINT自动删除
add type on your tsx file, to prevent removed automatically by vscode
.eslint