简体   繁体   English

如何在 Visual Studio Code 中格式化 React 代码?

[英]How to format React Codes in Visual Studio Code?

When auto-formatting React codes in VSCode it makes errors.在 VSCode 中自动格式化 React 代码时会出错。 I am using the Prettier extension.我正在使用 Prettier 扩展。 So what is the best code formatted for this?那么为此格式化的最佳代码是什么?

Prettier is extracly of you need. Prettier 正是您所需要的。 If you want to personalized format code jsx, you can configuration prettier with your style.如果你想要个性化的格式代码jsx,你可以根据自己的风格配置prettier。

Use vscode internal react formatter, it's very good:使用vscode内部的react formatter,很好用:

在此处输入图像描述

you can find both React & TypeScript React code formatter:你可以找到 React 和 TypeScript React 代码格式化程序:

在此处输入图像描述

Finally, I have found a way to do it.最后,我找到了一种方法。 In the vscode settings, I have to change to default format to prettier.在 vscode 设置中,我必须将默认格式更改为更漂亮。

在此处输入图像描述

As well as on the project it is needed to create a.prettierrc file with suitable configurations for the project.除了在项目上,还需要为项目创建一个具有合适配置的 .prettierrc 文件。

在此处输入图像描述 在此处输入图像描述

{
  "trailingComma": "es5",
  "tabWidth": 2,
  "semi": true,
  "singleQuote": true
}

And if you do not need it, you can always enable to format the code without the.prettierrc file.如果您不需要它,您可以随时启用在没有 .prettierrc 文件的情况下格式化代码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM