React Js noting summary
unpublished draft
ReactJsEsLint
Overview#
Hooks#
- useState
- useEffect
- useContext
- useCallback: basically cache the function, not the result of function.
- useMemo: cache the result of the function, not the function itself.
- useRef: Use with or without forwardRef
- useLayoutEffect: Prefer using classic
useEffect
, only use this if we have reasonable circumstance. This behavior just like a synchronous version ofuseEffect
, therefore, blocking visual could be appear. - useReducer: An alternatively complex version of
useState
, that instead of update value directly, it make use of dispatch style. Only prefer for complex logic process. - useImperativeHandle: using with
forwardRef
to expose some method from the child component to exposed on the ref itself. - useDebugValue: not using so much.
Setup#
General#
As the time writing this, react scripts was on v4.x and react v17
- IDE: VSCode
- Make sure install yarn first. We would use
yarn
all the way instead of npm. - Install Eslint and Prettier extensions.
Common Configs
{
"eslint.run": "onSave",
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"javascript.preferences.quoteStyle": "single",
"typescript.preferences.quoteStyle": "single",
"javascript.updateImportsOnFileMove.enabled": "always",
"typescript.updateImportsOnFileMove.enabled": "always",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
Consider settings
{
"css.lint.unknownAtRules": "ignore",
"scss.lint.unknownAtRules": "ignore",
}
Create React App#
Script to run
yarn create react-app [app-name] --template typescript # create template, using typescript
yarn add eslint prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y @typescript-eslint/eslint-plugin @typescript-eslint/parser --dev
yarn add husky lint-staged --dev
npx husky install # initialize husky
npx husky add .husky/pre-commit "yarn lint-staged" # config before commit, run lint stage with all commit file. (also depend on lint-staged command in package.json file)
Absolute path import
- Ensure having "baseUrl": "src" in compilerOptions section in tsconfig.json file
- Ensure having at least "include": ["src"] in tsconfig.json file
- Ensure having at least 'import/resolver': node: paths: ['src'] in settings section of .eslintrc file
- Go to setting UI, find for import module specifier, find Typescript section, change it back to auto or non-relative
- Command filter, find Restart TS Server then execute.
Explainations#
- eslint: prefer install per project for explicit
insists that the current project make use and depend on
eslint
. Make sure installingeslint
extension for VsCode first. - prettier: prefer install per project for explicit insists that the current
project make use and depend on
prettier
. Make sure installingprettier
extension for VsCode first. @typescript-eslint/eslint-plugin
: eslint plugin.@typescript-eslint/parser
: eslint parser for typescript.eslint-config-prettier
:prettier
config foreslint
, to avoid setting conflict.eslint-plugin-prettier
:prettier
plugin foreslint
.eslint-plugin-react
:react
plugin foreslint
.eslint-plugin-react-hooks
:react-hook
plugin foreslint
.- eslint-plugin-jsx-a11y: Static AST checker for accessibility rules on JSX elements.
Error while loading rule 'prettier/prettier'
As for the time writing this page, eslint already release version 8 and not all the plugin catchup yet, so if this error pop-up, try to downgrade eslint back to 7.32.0 then recursive upgrade its dependencies: yarn upgrade -R eslint
Example Files#
parserOptions > project > tsconfig file | overrides > parserOptions > project > tsconfig fileif present its probably instruction for vs code to trace back the file (which mean it's the folder context of vscode, not the project compiler)
Create Next App#
As the time writing this, react scripts was on v5.x, react v17 and NextJs 12
Script to run
yarn create next-app [Your App name] --ts
# eslint was added with ts template
yarn add prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y @typescript-eslint/eslint-plugin @typescript-eslint/parser --dev
yarn add husky --dev
yarn husky install
yarn husky add .husky/pre-commit "yarn lint-staged"
yarn add lint-staged --dev
# Add [lint-staged.config.js] file as config can be found in corresponding folder
Example Files#
Tip and Trick#
Import node module in Typescript without type definitions#
- Make a file call
decs.d.ts
in root folder. - Declare missing type definition modules there:
declare module '[yourModule]';
- Include that file in
tsconfig.json
,include
section. - Done, import as normal (no intellisense support!).
To avoid resetMock from node_modules add to packages.json
"jest": {
"resetMocks": false
}