gkiely / eslint-plugin-no-state-hooks

Disallow usage of useState or useReducer in your application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

eslint-plugin-no-state-hooks

Avoid using useState or useReducer

When using an architecture that separates your application state from your UI components (e.g. Flux), it may be desirable to forbid the use of local component state.

Examples

function MyComponent() {
  const [getState, setState] = useState(); // error
  const [state, dispatch] = useReducer(() => {}, undefined); // error
  return <></>;
}

Installation

npm i -D eslint-plugin-no-state-hooks

Usage

Install and enable typescript-eslint with type linting, see:

npm install -D @typescript-eslint/parser@latest @typescript-eslint/eslint-plugin@latest eslint@latest typescript@latest
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "tsconfigRootDir": ".",
    "project": ["./tsconfig.json"]
  },
  "plugins": ["@typescript-eslint"],

Configure the plugin in your .eslintrc:

{
  "extends": ["plugin:no-state-hooks/recommended"]
}

This essentially expands to:

{
  "plugins": ["no-state-hooks"],
  "rules": {
    "no-state-hooks/no-state-hooks": "error"
  }
}

Credit

Inspired by:

About

Disallow usage of useState or useReducer in your application

License:MIT License


Languages

Language:TypeScript 100.0%