124

I've the following snippet in my index.js

class App extends Component {
    render() {
        return ( <div style = { styles.app } >
            Welcome to React!
            </div>
        )
    }
}


The code works, but every time I save (ctrl+s) visual studio format the jsx like that:

class App extends Component {
    render() {
        return ( < div style = { styles.app } >
            Welcome to React!
            <
            /div>
        )
    }
}

How can I solve this? thanks

GalAbra
  • 5,048
  • 4
  • 23
  • 42
omer727
  • 7,117
  • 6
  • 26
  • 39
  • Do you have a formatting extension installed? I copied your code into a new js file and it formats correctly with vs code. So if you have an extension installed I would say uninstall it, if not then maybe you need to update vscode – ndonohoe Jul 09 '17 at 09:59

18 Answers18

359

In the end what did the trick was changing the file format from JavaScript to JavaScript React on the bottom toolbar. I'm publishing it here for future reference since I didn't find any documentation on this topic.

enter image description here

In addition to the above. If you click 'Configure File Association for .js' you can set all .js files to Javascript React

Jay
  • 65
  • 1
  • 8
omer727
  • 7,117
  • 6
  • 26
  • 39
  • is there a way to enable this by default on all js files. Say, maybe a settings/ config file – Kenshinman Apr 11 '18 at 10:38
  • @Kenshinman Would like to know this as well! I am only working with React but my files are all .js and not .jsx – Eric Bachhuber Apr 21 '18 at 18:20
  • 11
    thanks, you can also consider to put "files.associations": { "*.js": javascriptreact" } to the settings.json – khoailang Jul 10 '18 at 09:09
  • @khoailang Looks like that works for me, it just appears we are missing the opening " for the value of javascriptreact. Hope that helps. – technoplato May 03 '19 at 04:10
72

change vscode preferences settings > user settings below:

"files.associations": {
        "*.js":"javascriptreact"
    }
dotdot
  • 829
  • 6
  • 4
31

You can prevent VSC from incorrectly formatting your JSX by adding an association in your settings.json

Code > Preferences > Settings

In the settings window search for associations, click edit in settings.json and then add:

"files.associations": {
    "*.js": "javascriptreact"
}
jadeallencook
  • 686
  • 1
  • 8
  • 13
15

Alternatively, saving the file with a .jsx extension resolves this in vscode.

I had the same challenge and I am hoping to discover a better way of handling this issue in vscode.

I noticed your suggested work-around has to be done each time you open the react file with an extension of .js

twumm
  • 375
  • 1
  • 13
8

Open the Visual Studio Code Settings. Refer the image below to see how to navigate to the settings.

Open Vscode Settings

Once the settings tab is open. If you want to make the settings changes for all the projects then select the User sub tab, if only for current project then select the Workspace sub tab.

Add file Associations

type "file associations" in the search text box and press Enter.

Click on add item.

  • set Item : *.js
  • set Value : javascriptreact

Above changes will start associating all *js files in the project as javascript React files.

Next open any .js file in your project and right click and select Format Document. If you have multiple formatters then associate your favorite formatter. I have used Prettier to handle my formatting.

CostaIvo
  • 1,029
  • 13
  • 19
  • how do you change the default formatter? Mine is set to C#, but there is no formatter for react listed even though it's selected at the bottom of the VSCode window – MC9000 Feb 13 '22 at 19:40
  • 1
    Install the extension for Prettier from https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode In VSCode settings you can set the value for the file to set your default formatted as shown below { "editor.defaultFormatter": "esbenp.prettier-vscode", "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" } } – CostaIvo Feb 16 '22 at 06:51
5

Install Prettier (if not installed by default) and try to add this to your user or workplace settings:

"prettier.jsxBracketSameLine": true

Do not put linebreak between return and the returned JSX expression.

Trigger autoformat (Alt+Shift+F) and check if works.

emecas
  • 1,586
  • 3
  • 27
  • 43
Janos
  • 650
  • 8
  • 13
5

I struggled with this but finally found a solution. This is my settings.json

{
    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
    "workbench.startupEditor": "welcomePage",
    "window.zoomLevel": 1,
    "emmet.includeLanguages": {
        "javascript": "javascriptreact",
        "vue-html": "html"
    },
    "editor.formatOnSave": true,
    "javascript.updateImportsOnFileMove.enabled": "always",
    "editor.wordWrap": "on",
    "editor.tabSize": 2,
    "editor.minimap.enabled": false,
    "workbench.iconTheme": "vscode-icons",
    "eslint.autoFixOnSave": true,
    "eslint.alwaysShowStatus": true,
    "beautify.ignore": [
        "**/*.js",
        "**/*.jsx"
    ],
    "prettier.jsxSingleQuote": true,
    "prettier.singleQuote": true
}

I added

"beautify.ignore": ["**/*.js","**/*.jsx"]
Loc Mai
  • 217
  • 3
  • 9
5

Here is what worked for me-

I clicked on the Language mode (Javascript React) at the bottom of the screen

enter image description here

Then chose the Configure React Javascript Language based setting option

enter image description here

Then changed the javascript react default formatter to prettier as shown in the pic.

enter image description here

That pretty much did it for me after I saved the React file.

Snowcat
  • 470
  • 8
  • 16
4

Make sure you dont have multiple javascript formatters enabled in your current workspace. (You have to disable the rest of them for your current workspace).

react-beautify mostly does the magic but fails if you have some other JS formatter/beautifier already installed.

In my case, I had react-beautify and JS-CSS-HTML Formatter extension installed. I had to disable the JS-CSS-HTML Formatter for my current workspace.

Ozesh
  • 6,536
  • 1
  • 25
  • 23
  • how do you disable a formatter for a single workspace? – Chris May 23 '18 at 05:31
  • Here is how you do it > (1) Go to the Extensions section. (2) Locate the JS-CSS-HTML Formatter (3) Head over to the Disable drowdown menu (4) There must be a section saying Disable (Workspace) – Ozesh May 23 '18 at 11:33
2

I just added all the combinations mentioned above.

  1. added this
"files.associations": {
    "*.js": "javascriptreact"
}
  1. added this also
"beautify.ignore": ["**/*.js","**/*.jsx"]
  1. Deleted additional js formatting
  2. installed prettier
  3. turn ON prettier and formatting
glinda93
  • 7,659
  • 5
  • 40
  • 78
jamil_4128
  • 29
  • 1
1

You can install an extension like react-beautify that helps you format your jsx code.

It is found here

This extension wraps prettydiff/esformatter to format your javascript, JSX, typescript, TSX file.

Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
1

I had to disable the JS-CSS-HTML Formatter extension in VSC. only solution to this problem

1

Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary. include : JavaScript TypeScript Flow JSX JSON CSS SCSS Less HTML Vue Angular GraphQL Markdown YAML https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

Laxifan
  • 11
  • 1
  • 4
0

I had similar problem, then I found out it was caused by 'beautify' extension. After I uninstalled the extension, everything is working fine.

Akrom
  • 311
  • 2
  • 5
0

After reading many great suggestions and workarounds, I discovered that I could simply place my mouse arrow down over the bright blue horizontal bar at the bottom of VSCode editor window - right click - which opens a popup list window - and deselected = "Editor Indentation".

0

You can double click JavaScript in the Status Bar at the bottom of VSCode, and then change the format from JavaScript to React (Choose React in the Select language mode to associate with '.jsx')

Hervera
  • 584
  • 9
  • 17
0

Bottom bar in VS code

I found simple solution as the html tags we are using are jsx , so i changed javascript to javascript jsx for each .js file to save as jsx in bottom bar.

Disclaimer : May be this is an instant soltution for those who are just beginning with React , jsx.

-3

add this in your .js code,

/* prettier-ignore */
bguiz
  • 27,371
  • 47
  • 154
  • 243
Nadeem
  • 25
  • 2