4

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

1 Comment

This has worked for me afterall, I've used create-react-app to be sure of any syntax errors, when I changed the default formatter to prettier, it has added several spaces usign this format {" "}. Even though the default formatter is prettier, this old extension JS-CSS-HTML Formatter was preventing new files to be formatted with prettier. Uninstalling JS-CSS-HTML Formatter solved my problem. Thank you @guillermo-brachetta
0

Change the file type from javascript to javascript react

https://i.sstatic.net/f7hYJ.png

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.