I am using Prettier with format on save enabled but when I make any change in App.js and save it Prettier do destroy my code because it deals with it as plain javascript
-
2I don't see react in the list of supported languages for prettier, maybe simply this?Kaddath– Kaddath2019-01-31 14:19:30 +00:00Commented Jan 31, 2019 at 14:19
-
React isn't a language, so that could be it!Predrag Beocanin– Predrag Beocanin2019-01-31 14:22:55 +00:00Commented Jan 31, 2019 at 14:22
-
2It's common for another installed formatter to take precedence over prettier. Beautify is a common culprit.kemotoe– kemotoe2019-01-31 14:23:41 +00:00Commented Jan 31, 2019 at 14:23
-
I gave you a general answer since your question is so vague. Update it with more project information and I could provide a better answer.serraosays– serraosays2019-01-31 14:53:57 +00:00Commented Jan 31, 2019 at 14:53
-
kemotoe's comment is important. It's easy to install many 3rd-party extensions with overlapping functionality. Before anything else, one should inspect their VS Code extensions.Kalnode– Kalnode2020-01-12 14:10:02 +00:00Commented Jan 12, 2020 at 14:10
5 Answers
I am using create-react-app so it produce the main component App.js I am using .JSX in the other components but the problem is when I make any change to the App.js
I fixed this issue by changing the files associations setting in Vscode settings (specific to user workspace)
I just added this line
"files.associations": {
"*.js": "javascriptreact"
}
and worked perfectly for me.
3 Comments
/yourproject/.vscode/settings.json and paste above code there. Also make sure to add this directory in .gitignoreI don't read anything about react in prettier documentation
As far as I understand, react code should be put in .jsx files, not .js
https://reactjs.org/docs/introducing-jsx.html
React doesn’t require using JSX, but most people find it helpful as a visual aid when working with UI inside the JavaScript code
2 Comments
.js file because it is just a .js fileIf you are using ESlint as well you should follow these steps...
yarn add --dev --exact prettier
yarn add --dev eslint-plugin-prettier
Create a .eslintrc file in the root of your project with the following contents
{
"extends": "react-app",
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}
That should hopefully do the trick.
Comments
Prettier checks how your code is structured whereas VSCode syntax code highlighting controls how your code looks.
VS Code highlights code via extensions, not with Prettier. Out of the box, VS Code supports React and JSX natively. You also adjust VS Code's settings.json file. Here's an example off how that might look:
{
"window.zoomLevel": 0,
// Solarized-dark theme
"workbench.colorTheme": "Solarized-dark",
// Changes the highlight color in solarized-dark, which I can't see
"workbench.colorCustomizations": {
"editor.selectionBackground": "#5b455e",
"editorBracketMatch.border": "#555",
"editorBracketMatch.background": "#5b455e"
},
// Number of spaces in a tab
"editor.tabSize": 2,
// Insert spaces when pressing Tab. This setting is overriden
// based on the file contents when `editor.detectIndentation` is true.
"editor.insertSpaces": true,
// Override whatever the files say and give me 2 spaces per tab
"editor.detectIndentation": false,
"workbench.startupEditor": "newUntitledFile",
"explorer.confirmDelete": false,
"explorer.confirmDragAndDrop": false,
"javascript.updateImportsOnFileMove.enabled": "always"
}
Prettier code formatting is usually controlled as part of a server, rolled up into something like Webpack or Parcel as part of a build script. If you're using create-react-app I think it's already wired in. If you want to adjust some of the settings, Prettier uses a simple JSON file called .prettierrc that belongs in the root of your project.
Example .prettierrc:
// Relaxes need for semicolons, lints for single quotes and requires trailing commas
{
"semi": false,
"singleQuote": true,
"trailingComma": "es5"
}