最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Formatter in VsCode for React - Stack Overflow

programmeradmin0浏览0评论

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.

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"
          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"
        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.

Share Improve this question edited Jan 9, 2023 at 12:50 Maytham Fahmi 33.5k16 gold badges127 silver badges153 bronze badges asked Jan 4, 2023 at 20:49 gregoire constantgregoire constant 211 silver badge2 bronze badges 3
  • Which one of those is "strange" and what would you expect? – A_A Commented Jan 4, 2023 at 20:52
  • 2 @A_A The second one is obviously stranger than the first one... – Guillaume Brunerie Commented Jan 4, 2023 at 21:14
  • @A_A Because for some inexplicable reason, Prettier et al decide that, since the code being formatted is JSX, it's going to simply discard indentation on the HTML-in-JS, while simultaneously treating (from a formatting perspective) every < as an operator (but not >; that would be silly) instead of the &lt; that behaviorally it should see it as. The formatter (again, I think Prettier, in this case) sees the file extension is .JSX, and appears to just throw its hands up when it encounters the embedded HTML (which really is a "sub-language" in this instance, necessitating its own formatter) – NerdyDeeds Commented Oct 31, 2023 at 11:54
Add a ment  | 

3 Answers 3

Reset to default 3

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!

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.

发布评论

评论列表(0)

  1. 暂无评论