⚙ Configure ESLint and Prettier in Visual Studio Code for React Developers 🔥
-
Install ESLint VSC Extension…
crtl+p ➡
ext install dbaeumer.vscode-eslint➡ Enter -
Install ESLint into your React project…
npm install eslint --save-dev -
Create
eslintrc.jsonThov…npx eslint --init -
Add rules to the
eslintrc.jsonThov
"rules": {
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
"react/react-in-jsx-scope": "off",
"react/jsx-one-expression-per-line": [0, { "allow": "literal" }],
"no-unused-vars": [1, { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }] }
For documentation for ESLint rules, go to ESLint site
-
Create a VSC settings file in the root of your React project…
File ➡ Save Workspace As ➡ Save
Add the following lines to settings in
workspace.code-workspaceThov…
//eslint settings
"editor.codeActionsOnSave": {
"source.fixAll": true
}
-
Install the Prettier extension…
crtl+p ➡
ext install esbenp.prettier-vscode➡ Enter -
Install Prettier into your project…
npm install --save-dev --save-exact prettier -
Create a
.prettierrc.jsonfile to your React project root to create custom rules for Prettier.Add the following rules to the file…
{
"trailingComma": "es5",
"semi": true,
"singleQuote": true,
"jsxSingleQuote": true,
"printWidth": 120
}
For documentation for Pretter rules, go to Prettier site
-
Add the following settings to
workspace.code-workspace, to format code on saving. Also disables Prettier on js and React files, as we are using ESLint for these.
//prettier settings
"editor.defaultFormatter": "esbenp.prettier-vscode",
"prettier.disableLanguages": ["javascript", "javascriptreact"],
"editor.formatOnSave": true,
