简体   繁体   English

有没有办法在不删除'react import'的情况下在vs代码中组织导入

[英]Is there a way to oraginize imports in vs code without removing 'react import'

I am trying to organize imports on saving a file.我正在尝试在保存文件时组织导入。 So I updated vs code settings to always organize imports when saving a file.所以我更新了 VS 代码设置,以便在保存文件时始终组织导入。

But it also removes import React from 'react' .但它也import React from 'react'

So react gives me this error 'React' must be in scope when using JSX .所以 React 给我这个错误'React' must be in scope when using JSX

For eg,例如,

import React from 'react'

const Temp = () => {
  return (
    <div>Temp</div>
  )
}

export default Temp

organizes to组织到

const Temp = () => {
  return <div>Temp</div>;
};

export default Temp;

This is my react version - "react": "^16.13.1" .这是我的 React 版本 - "react": "^16.13.1"

Have you tried using a babel.config.js file?您是否尝试过使用babel.config.js文件?

module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                modules: false,
            },
        ],
        ['@babel/preset-react', { runtime: 'automatic' }],
    ],
};

I have a project that uses this and it works pretty fine.我有一个使用它的项目,它工作得很好。

Refer the docs for configuration.参考文档进行配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在VS Code中组织导入会删除我的预先导入 - Organize imports in VS Code removes my preact import 如何在 React 应用程序中为绝对路径导入启用 VS 代码智能感知? - How to enable VS code intellisense for absolute path imports in a React app? ES6导入React命名导入的最佳方式 - ES6 best way to import React's named imports 如何在 VS Code 中的 React 项目中启用导入自动完成而不需要检查打字稿 - How to enable import autocomplete without typescript checking in a React project in VS Code VS Code 自动删除“import React from &#39;react&#39;”语句 - VS Code auto-removes the "import React from 'react'" statement 如何在 React、VS 代码中停止导入具有相对路径的节点模块 - How to stop import of node modules with relative paths in React, VS code 为什么我的VS Code中的绝对反应导入会不时自动转换为相对? - Why does my absolute react imports in VS Code automatically convert to relative from time to time? 有没有办法在没有 create-react-app 的情况下在 React 中导入模块? - Is there a way to import modules in React without create-react-app? 在React中处理多个导入的有效方法 - Efficient way to handle multiple imports in React 有没有办法在没有脚本标签的情况下将 Google Maps API 导入 react? - Is there a way to import the Google Maps API into react without a script tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM