简体   繁体   English

如何在 VSCode 中使用 Prettier 修复 JSX/React 格式问题

[英]How to fix JSX/React formatting issue with Prettier in VSCode

This is how I want my code to format这就是我希望我的代码格式化的方式

render() {
  return (
    <div>
      {this.state}
    </div>
  )
}

But when I alt+shift+F to format with prettier, I get this (which is incredibly frustrating!!)但是当我 alt+shift+F 以更漂亮的格式格式化时,我得到了这个(这令人难以置信的沮丧!!)

render() {
  return <div>{this.state}</div>;
}

It's not TOO bad in this particular situation, but I'd rather fix it now before I get into anything too complicated.在这种特殊情况下并不算太糟糕,但我宁愿现在解决它,然后再陷入任何太复杂的事情。

Does anyone know which settings I can can change to fix this?有谁知道我可以更改哪些设置来解决这个问题? I've been looking around for a while and can't find anything helpful.我已经四处寻找了一段时间,找不到任何有用的东西。

Prettier is an opinionated formatter so there isn't a lot of flexibility outside of these configuration settings . Prettier 是一个固执己见的格式化程序,因此在这些配置设置之外没有太多的灵活性。

I wouldn't worry though, the reason its formatted like that is because it can fit all of it on a single line without going outside of the column range of 100, if you try adding another div or something you'll notice that it will not do this, and so more complex scenarios is not going to be problematic.不过我不担心,它格式化的原因是因为它可以将所有内容放在一行中而不会超出 100 的列范围,如果您尝试添加另一个 div 或其他内容,您会注意到它会不这样做,因此更复杂的场景不会有问题。

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

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