Guide to configure react app. Stack:
- Update
create-react-app
# run uninstall scrpt to uninstall current version of CRA
npm uninstall -g create-react-app
# or
yarn global remove create-react-app
# install latest version of CRA
npm install -g create-react-app
#or
yarn global add create-react-app
- Run
create-react-app
npx create-react-app my-app
# or
npx create-react-app my-app --template typescript
cd ./my-app
npm install
npm start
For more info visit https://facebook.github.io/create-react-app/docs/getting-started
- Install typescript and types for default packages.
npm install typescript @types/node @types/react @types/react-dom @types/jest --save-dev
# or
yarn add typescript @types/node @types/react @types/react-dom @types/jest --dev
- rename your
.js
files to.tsx
in case components or.ts
in case pure typescript files
For more info visit https://create-react-app.dev/docs/adding-typescript/
- Redux
npm install redux
npm install @types/react-redux --save-dev
# or
yarn add redux
yarn add @types/react-redux --dev
- Redux Saga
npm install redux-saga
# or
yarn add redux-saga
- React Query
npm install react-query
# or
yarn add react-query
- Install Router
npm install react-router react-router-dom
npm install @types/react-router-dom --save-dev
# or
yarn add react-router react-router-dom
yarn add @types/react-router-dom --dev
- Samples
<Switch>
<Route
path={'/test-route-1'}
render={() => (
<Layout
content={<YourContainer1 />}
cannotAccesView={cannotAccesView}
/>
)}
/>
<Route
path={'/test-route-2'}
render={() => {
if (cannotAccesView) {
return <Redirect to="/" />;
}
return <Layout content={<YourContainer2 />} />;
}}
/>
<Route
path={'/test-route-3'}
render={() => {
if (condition1) {
return <Layout content={<YourContainer1 />} />;
}
if (condition2) {
return <Layout content={<YourContainer2 />} />;
}
return <Layout content={<YourContainer3 />} />;
}}
/>
</Switch>
Why ESLint? https://eslint.org/blog/2019/01/future-typescript-eslint
npm install @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-airbnb eslint-plugin-react eslint-plugin-react-redux eslint-plugin-redux-saga eslint-plugin-react-hooks eslint-plugin-import --save-dev
- create
.eslintrc.json
file for configuration
{
"extends": [
"airbnb",
"plugin:@typescript-eslint/recommended",
"plugin:react-redux/recommended",
"plugin:redux-saga/recommended"
],
"plugins": ["react-hooks", "react-redux", "redux-saga", "@typescript-eslint"],
"env": {
"browser": true,
"jasmine": true,
"jest": true
},
"rules": {
"react/jsx-handler-names": 1,
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
"redux-saga/no-unhandled-errors": "off",
"react-redux/prefer-separate-component-file": "off",
"@typescript-eslint/interface-name-prefix": ["warn", "always"],
"@typescript-eslint/explicit-function-return-type": 1,
"import/order": [
"error",
{
"pathGroups": [
{
"pattern": "react",
"group": "external",
"position": "before"
},
{
"pattern": "./styles",
"group": "sibling",
"position": "after"
}
],
"groups": ["builtin", "external", "internal", "parent", "sibling", "index"],
"pathGroupsExcludedImportTypes": ["react"],
"newlines-between": "always",
"alphabetize": {
"order": "asc",
"caseInsensitive": true
}
}
]
},
"settings": {
"react": {
"pragma": "React",
"version": "detect"
}
},
"parser": "@typescript-eslint/parser"
}
- Create
.eslintignore
src/registerServiceWorker.js
src/**/__tests__/**
webpack/*
- Update
scripts
section in yourpackage.json
"scripts": {
"lint:fix": "eslint './src/**/*.{ts,tsx}'"
}
# or you can use your local esslint
npm i -g eslint
- Run linter
npm run lint:fix
# or you can use your local esslint
eslint --ext .ts,.tsx --fix --debug source/path-to-directory
eslint --fix --debug source/path-to-file/Component.tsx
- You need to have
TSLint
configured npm install tslint-config-prettier --save-dev
npm install tslint-plugin-prettier --save-dev
- Update your
tslint.json
file
{
"extends": [
"tslint:recommended",
"tslint-react",
"tslint-config-prettier"
],
"rulesDirectory": [
"tslint-plugin-prettier"
],
"rules": {
"prettier": true
}
}
- Create
.prettierrc
{
"trailingComma": "none",
"singleQuote": true,
"printWidth": 120
}
- Update
scripts
section in yourpackage.json
{
"scripts": {
"tslint-check": "tslint-config-prettier-check ./tslint.json",
"prettier": "npm run tslint-check && prettier --write \"*/**/*.{ts,tsx,less}\""
}
}
- Run
npm run prettier