The application has been bootstrapped using Create React App
for simplicity reasons. It allows us to create applications quickly without dealing with a complex tooling setup such as bundling, transpiling etc.
You should always configure and use the following tools:
ESLint is a linting tool for JavaScript. By providing specific configuration defined in the.eslintrc.js
file it prevents developers from making silly mistakes in their code and enforces consistency in the codebase.
ESLint Configuration Example Code
This is a great tool for formatting code. It enforces a consistent code style across your entire codebase. By utilizing the "format on save" feature in our IDE you can automatically format the code based on the configuration provided in the .prettierrc
file. It also gives us good feedback when something is wrong with the code. If it doesn't auto-format, something is wrong with the code.
Prettier Configuration Example Code
ESLint is great for catching some of the bugs related to the language, but since JavaScript is a dynamic language ESLint cannot check data that run through the applications, which can lead to bugs, especially on larger projects. That is why TypeScript should be used. It is very useful during large refactors because it reports any issues we might have missed otherwise. When refactoring, change the type declaration first, then fix all the TypeScript errors throughout the project and you are done. One thing you should keep in mind is that TypeScript does not protect your application from failing during the runtime, it only does type checking during the build time, but it increases development confidence drastically anyways. Here is a great resource on using TypeScript with React.
Husky is a tool for executing git hooks. Use Husky to run your code checking before every commit, thus making sure the code is in the best shape possible at any point of time and no faulty commits get into the repo. It can run linting, code formatting and type checking, etc. before it allows pushing the code. You can check how to configure it here.
Absolute imports should always be configured and used because it makes it easier to move files around and avoid messy import paths such as ../../../Component
. Wherever you move the file, all the imports will remain intact. Here is how to configure it:
For JavaScript projects:
// jsconfig.json
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
For TypeScript projects:
// tsconfig.json
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
Paths Configuration Example Code
In this project we have to create another tsconfig file tsconfig.paths.json
where we configure the paths and merge it with the base configuration, because CRA will override it otherwise.
It is also possible to define multiple paths for various folders, but using @/*
works very well because it is short enough so there is no need to configure multiple paths and it differs from other modules so there is no confusion in what comes from node_modules
and what is our source files. That means that anything in the src
folder can be accessed via @
, e.g some file that lives in src/components/MyComponent
can be accessed using @/components/MyComponents
.