Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does jsx code formatting not correctly

vscode is my favorite editor, I have a code formatting problem with it. Here is my jsx code:

return <div className={panelHeadingClassName}>     <h3 className={style.panelTitle}>         <i className={iconStyle[iconClass]}></i>         {title}         <small className={style.panelSubTitle}>{subTitle}</small>     </h3> </div> 

when I use option + shift + F to format it, it gives me this:

return <div className = {   panelHeadingClassName } > < h3 className = {   style.panelTitle } > < i className = {   iconStyle[iconClass] } > < /i> {   title } < small className = {   style.panelSubTitle } > {   subTitle } < /small> < /h3> < /div> 

Obviously, it's not correct.

vscode version - v1.10.2, and not install any code formatting extension.

So, I don't want to install any code formatting extension, is there any config for vscode to doing this correctly?

-- update --

Sorry about that. I check my extensions installed, found Beautify extension. But why the i use vscode code formatting, it use Beautify?

like image 452
slideshowp2 Avatar asked Mar 21 '17 07:03

slideshowp2


People also ask

How do I beautify a JSX file?

You can use hot key Ctrl+M (Command+M) to beautify your JSX file.

What format is JSX?

JSX stands for JavaScript XML. It is simply a syntax extension of JavaScript. It allows us to directly write HTML in React (within JavaScript code). It is easy to create a template using JSX in React, but it is not a simple template language instead it comes with the full power of JavaScript.

How do I fix code formatting in Visual Studio?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.

What is the correct syntax to write expression in JSX?

Embedding Expressions in JSX const name = 'Josh Perez';const element = <h1>Hello, {name}</h1>; You can put any valid JavaScript expression inside the curly braces in JSX. For example, 2 + 2 , user. firstName , or formatName(user) are all valid JavaScript expressions.


1 Answers

As you note, the Beautify extension is the root cause here (see this issue). That extension provides a document formatter that VSCode will run when you run the format command

A few options:

  • Disable the extension
  • Disable Beautify for just js files by removing the js entry from the "beautify.language" setting
  • Make sure your file has a language mode of javascriptreact. I believe this will prevent Beautify from running on the file
like image 199
Matt Bierner Avatar answered Oct 14 '22 00:10

Matt Bierner