admin管理员组文章数量:1022452
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<
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
3 Answers
Reset to default 3Follow these steps:
CTRL + SHIFT + P
- Format Document (in pop-up bar)
- Select
Format Document
- Select
Configure Default Formatter...
- Select
Prettier - Code formatter
To enable Format On Save
CTRL + SHIFT + P
- Preferences Open Settings (UI) (in pop-up bar)
- Type
Format On Save
- 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.
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<
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
3 Answers
Reset to default 3Follow these steps:
CTRL + SHIFT + P
- Format Document (in pop-up bar)
- Select
Format Document
- Select
Configure Default Formatter...
- Select
Prettier - Code formatter
To enable Format On Save
CTRL + SHIFT + P
- Preferences Open Settings (UI) (in pop-up bar)
- Type
Format On Save
- 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.
本文标签: javascriptFormatter in VsCode for ReactStack Overflow
版权声明:本文标题:javascript - Formatter in VsCode for React - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745572903a2156844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论