VSCode bad javascript formatting

All we need is an easy explanation of the problem, so here it is.

I am editing some javascript files (particularly, reactjs coed) in VS Code on Ubuntu 18.04. However, the “formatting” is really terrible.

Before:

import React, { Component } from 'react';
import './App.css';
import Dropzone from 'react-dropzone';

class App extends Component {

    render() {
        return (
            <div className="App">
                <Dropzone onDrop={this.onDrop} />
            </div>
        );
  }
}

export default App;

After:

import React, {
    Component
} from 'react';
import './App.css';
import Dropzone from 'react-dropzone';

class App extends Component {

    render() {
        return ( <
            div className = "App" >
            <
            Dropzone onDrop = {
                this.onDrop
            }
            /> <
            /div>
        );
    }
}

export default App;

Previously, I was editing these files on Windows 10 in VS Code, and the formatter was great. Is there an extension I am missing? Or what am I doing wrong here. To format, I am using the “Format Document” keyboard shortcut.

Here are my current extensions:

enter image description here

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The culprit for this behavior was Beatify extension on Vscode in my case. Disabling resolved the issue.

Method 2

Here is the solution,

  1. change the language manually to "javaScript React"

VSCode bad javascript formatting

  1. try with your favorite formatter(you can install vs code extensions such as "Prettier","beautify")

Cheers !

Method 3

I personally use Prettier for JS and CSS formatting, and JS JSX Snippets for JSX in React. I have tried a lot of others, but with those 2 I can assure you your React code will look beautiful.

Method 4

hey js jsx snippest works, extension Id of it in VS Code is skyran.js-jsx-snippets or you can simply search by typing js jsx snippest. install it, after installing it, go into format document with..->configure default formatter->js jsx snippest make it default and then it will format appropriatly (you can format with it without making it default but if you make it default you just need to click format document insted of selecting formatter every time.)

if at time of saving file your formatted text is getting deformated then you need to disable or uninstall other formatters like JS-CSS-HTML formatter,beautify,beautify css/sass/scss/less, etc.

Method 5

I personally recommend JS-CSS-HTML Formatter extension by Lonefy.

Plz refer to link here.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply