VSCode adds spaces inside HTML tags inside React code

Viewed 842

Starting to learn React from ground when I saved the code below VS Code formatted it as in the image.

ReactDOM.render(
    <div>
        <h1> Hi there  {name} </h1> 
        <p>  your lucky number is {num} </p>
    </div>,
    document.getElementById("root")
);

Line breaks with spaces formatted sample code from VSCode: enter image description here

I've googled it but some time later I realized that it was the file format that caused this problem.

Thanks Guillermo, the VSCode extension JS-CSS-HTML Formatter was the cause, uninstalling it and using prettier instead solved the problem.

2 Answers

I solved by the following steps:

  • Install the Prettier-Code formatter.
  • Disable the default HTML formatter in settings.
  • After that restart your VS-Code.

For more detail please view the under link:

How to disable the default HTML formatter.

Maybe you're using a formatter for js files that doesn't get well along with React.

You could try to add this line to your settings.json file:

"[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
}

(Provided you have Prettier installed)

Related