Making a new project ready for development can be a little overwhelming, especially if you use React, Typescript, ESLint, and Prettier, but today I am going to help you set up your React project correctly in a simple way.
Usually, the problem is that every time you have to set up ESLint and Prettier you go to the web search for an article, and this article is probably not updated. Usually, they use a lot of plugins and non-default configurations.
We are going to use the basic configuration of ESLint and Prettier to have fewer headaches.
Create react app:
--template typescript
: used to create a ReactJS TypeScript project instead of JavaScript.
What is ESLint?
ESLint is a JavaScript linting open source project and is used to find problems and syntax issues in your code, it will help us find broken logic that would be found only in run time.
To install ESLint:
After installing the ESLint we have to initialize the config file:
Here we are going to answer some questions about our project:
Choose ‘To check syntax and find problems’ because later we are going to use Prettier to enforce code style as well.
Choose JavaScript modules mainly because ReactJS uses them.
Choose ‘React’ as our framework.
Choose ‘Yes’ for TypeScript.
Choose ‘Browser’ because ReactJS runs on the Browser.
Here you are free to choose any of the options, but I am going to use JSON as my format.
If you are using NPM then just select ‘Yes’, if not select ‘No’ then copy the packages and:
Right now the ESLint package created a .eslintrc file with the format you choose:
Before continuing we have to install TypeScript plugins related to ESLint and to do so:
if everything went well your eslintrc file should look something like this:
Let’s install prettier and then edit the eslint and prettier configuration files.
Before all, what is Prettier?
Prettier is a code style formatter, different from ESLint, Prettier only format the code style, and does not look for syntax problems.
Rules like max-len, no-mixed-spaces-and-tabs, keyword-spacing, comma-style
are some popular formatting rules in Prettier.
And rules like no-unused-vars, no-extra-bind, no-implicit-globals, prefer-promise-reject-errors
are common rules in ESLint.
In this case, I prefer ESLint to search for problems and syntax errors and let the code style for prettier.
To install Prettier:
After installing you have to create the prettierc file:
At this point you have a blank .prettierrc file and a default eslintrc file, so the next step is to configure the eslintrc file.
Open your eslintrc file.
If you are going to use or you intend to use Jest in your project add the next lines to your ‘env’:
To use prettier alongside with eslint you need to change the extends object:
The main part of ESLint is the rules objects. Here you can put any rule you think is good for your project and team.
My basic configuration is this:
If you want to learn more about ESLint rules you can check out rules page.
To use the plugins we have installed, update the plugins object in the eslintrc file:
The last thing to set up in ESLint is the eslint-import-resolver-typescript
. Just add the settings key in the eslint configuration file.
Lastly, we are going to set up the .prettierrc file created at the begging of the article.
My basic configuration for prettierrc file is:
But if you want to learn more check out the options page.
Finally, we have to add the scripts in the package.json:
Basically:
- lint: will search for problems, but will not fix
- lint fix: will search and try to fix the problems.
- format: will call prettier to fix the code style.
At this point, you probably have a fully working ReactJS project with linting. If you encounter some error or problem don’t hesitate in commenting below.
And if you like content about Git, Linux, Productivity tips, Typescript, and Python please follow me on Medium Marco Antonio Bet or checkout itsbetma.com.
If you find this article useful share it with your friends and save it for later.
See you soon.