Formatter in Vscode for React

When I save or format my React code with Ctrl + Shift + F the code below, is formatted in a strange way.

How can I solve this?

Default code:

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href=""
          target="_blank"
          rel="noopener noreferrer"
        >
          Hello CodeCademy
        </a>
      </header>
    </div>
  );
}

export default App;

After formatting the code:

import logo from './logo.svg';
import './App.css';

function App() {
    return ( <
        div className = "App" >
        <
        header className = "App-header" >
        <
        img src = { logo }
        className = "App-logo"
        alt = "logo" / >
        <
        p >
        Edit < code > src / App.js < /code> and save to reload. <
        /p> <
        a className = "App-link"
        href = ""
        target = "_blank"
        rel = "noopener noreferrer" >
        Hello CodeCademy <
        /a> <
        /header> <
        /div>
    );
}

export default App;

I tried to install another formatter and go to my settings to remove my "html" "twig" extension in emmet.

3

3 Answers

Follow these steps:

  1. CTRL + SHIFT + P
  2. Format Document (in pop-up bar)
  3. Select Format Document
  4. Select Configure Default Formatter...
  5. Select Prettier - Code formatter

To enable Format On Save

  1. CTRL + SHIFT + P
  2. Preferences Open Settings (UI) (in pop-up bar)
  3. Type Format On Save
  4. Enable Format On Save if it is not checked

Done!

1

Download the Prettier extension for VSCode. Follow this article.

I have had the same issue, even thus I have installed the Prettier extension. It shows later that I have had another extension called JS-CSS-HTML Formatter. I uninstalled JS-CSS-HTML Formatter and kept Prettier, and all problem was solved.

This issue is mentioned here as well.

Your Answer

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

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest