2. How to set up ESLint for React Projects?
ESLint itโs a linting tool that finds and many times fixes problems in
your JavaScript code. So, in this article, we will see how to set up ESLint
for React Projects.
The first question that will arrive in your mind is why to use ESLint. Letโs
say your code is running anyway without ESLint so whatโs great about
it? While working on project in-group biases can occur about the
proper code syntax or its styling. So, you will eventually end up
focusing on those problems. But we have a tool that will do that work
and you can just focus on problem-solving and building the project.
3. โข Installation: Install ESLint in your React Project as a dev Dependency
by running the following command:
npm install -D eslint
4. โข Configuration: You can configure ESLint according to your use case.
There are two ways two configure ESLint :
โข Configuration Comments: These are JavaScript comments which are
embedded into individual files to configure them
โข Configuration File: ESLint will use JavaScript/JSON/YAML file which
contain information to configure the entire directory.
โข In this particular config, we will use JSON format i.e. `.eslintrc.json` to
have our configurations, or else you can create the
`eslintConfig` property in `package.json` and write these
configurations in that property.
5. Properties in `.eslintrc.json`:
โข 1) โextendsโ and โpluginsโ:
โข By adding a file name in extends property we can inherit its
configuration, whereas โpluginโ works as an extension to ESLint
which can perform numerous functions.
Inside our `.eslintrc.json`file add extends and plugin property similar
to given below:
7. โข Note that as we have added various plugins we need to first install
them so run the following command to install them as
devDependencies :
npm install -D eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react
8. โข The โimport plugin will help us identify common problems while
importing and exporting; โjsx-a11yโ will catch errors regarding
accessibility and the โreactโ plugin is all about code practices used in
React, since we are using โslint-plugin-reactโ we will need to inform it
which version of React we are using so letโs add this in our
โsettingsโ property, instead of stating the current React version we will
handover this job to settings by using the keyword โdetectโ so that it
will detect the current React version from โpackage.jsonโ.
10. 2) โrulesโ:
โข Rules are used for configuring purposes, you can see all the rules that
you can use https://eslint.org/docs/rules/. You can set the error level
of rules in three different types :
โข โoffโ or 0: This will turn off the rule.
โข โwarnโ or 1: This will turn the rule on as a warning.
โข โerrorโ or 2: This will turn on the rule as an error.
โข Letโs add some rules to our config, you can add any other rules as per
your choice from the list of all rules mentioned above.
12. 3) โenvโ and โparserOptionsโ:
โข In the โenvโ property, we will specify what environments we are
working in. In parserOptions, we can specify JavaScript options like jsx
support or ecma version.
"parserOptions": {
"ecmaVersion": 2021,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"env": {
"es6": true,
"browser": true,
"node": true
},
13. โข The โlintโ command will run ESLint inside every file in the โsrc/โ, even
if your โsrc/โ folder contains multiple directories in it, this regex
command will go down recursively on them and run ESLint; If some
problems are reported by ESLint which are auto-fixable, then โlint:fixโ
command will do those auto-fixes.
14. Conclusion:
โข So, in this article, we have been through how to set up ESLint for
React Projects. Also, feel free to comment with your suggestions and
feedback on the post. Moreover, at BOSC Tech Labs, we have a team
of highly experienced React JS developers. They can assist you in
developing your customized web app. So contact us to hire
experienced ReactJS developers.
โข Article Source: https://bosctechlabs.com/set-up-eslint-for-react-
projects/