Visual studio code changes format (React-JSX)

ReactjsVisual Studio-CodeJsx

Reactjs Problem Overview


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

Reactjs Solutions


Solution 1 - Reactjs

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

Solution 2 - Reactjs

change vscode preferences settings > user settings below:

"files.associations": {
        "*.js":"javascriptreact"
    }

Solution 3 - Reactjs

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"
}

Solution 4 - Reactjs

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

Solution 5 - Reactjs

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.

Solution 6 - Reactjs

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"]

Solution 7 - Reactjs

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.

Solution 8 - Reactjs

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.

Solution 9 - Reactjs

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

Solution 10 - Reactjs

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.

Solution 11 - Reactjs

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

Solution 12 - Reactjs

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

Solution 13 - Reactjs

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".

Solution 14 - Reactjs

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')

Solution 15 - Reactjs

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

Solution 16 - Reactjs

Extensions I use

Not allowed to post a picture yet, but this is the extensions I use.
The first one is called Beautify - helps with HTML, CSS, JS
The second one is rc-beautify - helps with TS, JS, React.

Also Babel JavaScript is used as the language mode.

Solution 17 - Reactjs

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.

Solution 18 - Reactjs

add this in your .js code,

/* prettier-ignore */

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionomer727View Question on Stackoverflow
Solution 1 - Reactjsomer727View Answer on Stackoverflow
Solution 2 - ReactjsdotdotView Answer on Stackoverflow
Solution 3 - ReactjsjadeallencookView Answer on Stackoverflow
Solution 4 - ReactjstwummView Answer on Stackoverflow
Solution 5 - ReactjsJanosView Answer on Stackoverflow
Solution 6 - ReactjsLoc MaiView Answer on Stackoverflow
Solution 7 - ReactjsOzeshView Answer on Stackoverflow
Solution 8 - ReactjsIvix4uView Answer on Stackoverflow
Solution 9 - Reactjsjamil_4128View Answer on Stackoverflow
Solution 10 - ReactjsTal AvissarView Answer on Stackoverflow
Solution 11 - ReactjsAkromView Answer on Stackoverflow
Solution 12 - ReactjsJamalGhafariView Answer on Stackoverflow
Solution 13 - ReactjsTim ConroyView Answer on Stackoverflow
Solution 14 - ReactjsHerveraView Answer on Stackoverflow
Solution 15 - ReactjsLaxifanView Answer on Stackoverflow
Solution 16 - Reactjskarina ivanovaView Answer on Stackoverflow
Solution 17 - ReactjsSnowcatView Answer on Stackoverflow
Solution 18 - ReactjsNadeemView Answer on Stackoverflow