简体   繁体   English

如何配置 Prettier 格式可用的 React 代码?

[英]How do I configure Prettier format usable React code?

So I started learning React from this ( https://youtu.be/Dorf8i6lCuk?t=1946 ) tutorial and got stuck trying to save a file.所以我开始从这个 ( https://youtu.be/Dorf8i6lCuk?t=1946 ) 教程中学习 React,但在尝试保存文件时遇到了困难。 The video recommends getting prettier and I did and then when I save the file it auto-formats code into something that doesn't work.该视频建议变得更漂亮,我做到了,然后当我保存文件时,它会自动将代码格式化为不起作用的内容。 eg:例如: 格式错误的保存文件示例

as per the suggestion a jsx file:根据建议 jsx 文件: 在此处输入图片说明

as per suggestion 2: already in Javascript React:根据建议 2:已经在 J​​avascript React 中: 在此处输入图片说明

As per the suggestion in the solutions:根据解决方案中的建议:

在此处输入图片说明

Try going to settings > Extensions > uncheck - Enable/disable checkbox.尝试转到设置 > 扩展 > 取消选中 - 启用/禁用复选框。 Or Remove all together.或一起删除。

Change code to: (Notice the differences in my code to yours, your div tags are open and return does not have the divs wrapped in () .将代码更改为:(注意我的代码与你的不同,你的div标签是打开的, returndivs没有包含在()

function App() {
  return (
    <div>
      Hello!
    </div> //this was giving you the error before 
  )
}

export default App

I'd also double check your version of react that you are running.我还会仔细检查您正在运行的 react 版本。 If the version is different from the Tutorials, this could cause issues.如果版本与教程不同,这可能会导致问题。

This might help: Prettier's format on save messes up .jsx files这可能会有所帮助: Prettier 的保存格式会弄乱 .jsx 文件

Following @AlexB801 's advice I have fixed the thing.按照@AlexB801 的建议,我已经解决了这个问题。 It turns out you must NOT change the extension to .jsx but only change the formatter.事实证明,您不能将扩展名更改为 .jsx,而只能更改格式化程序。 Attaching an image.附上一张图片。 Also another thing to be noted here is that this was not an issue with Prettier.这里要注意的另一件事是,这不是 Prettier 的问题。 like so:像这样: 在此处输入图片说明

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

相关问题 如何让 Prettier 格式化我的代码并继续缩进? - How do I make Prettier format my code with indentation continuation? 如何为 React Native 更改更漂亮的格式 - how to change prettier format for react native 如何防止代码包装在 Prettier / ESLint - How do I prevent code wrapping in Prettier / ESLint 如何让用户更漂亮地在客户端格式化代码? - How to user prettier to format code on the clientside? 我想知道如何防止 Prettier 以这种奇怪的形式格式化我的 js 代码 - I wish to know how I can prevent Prettier to format my js code in this strange form 什么是 VS Code 中的 Prettier 键盘快捷键命令,用于仅格式化 React 代码块,而不仅仅是在文件自动保存时格式化? - What is Prettier keyboard shortcut command in VS Code to format only a block of React code, not just format on file autosave? VS Code:如何阻止 Prettier 在我的 JS 文件末尾添加新行? - VS Code: How do I stop Prettier from adding a new line at the end of my JS file? 为什么 Prettier 不在 VS Code 中格式化代码? - Why does Prettier not format code in VS Code? 如何只在我想要提交的文件上运行 Prettier? - How Do I Run Prettier Only on Files That I Want to Commit? 更漂亮的格式代码由我编写,而不是整个团队 - Prettier format code write by me, not the whole team
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM