Home > Blockchain >  formatter in VsCode for React
formatter in VsCode for React

Time:01-05

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="https://reactjs.org"
          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 = "https://reactjs.org"
        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.

CodePudding user response:

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!

CodePudding user response:

Download the Prettier extension for VSCode. Follow this article.

CodePudding user response:

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.

  • Related