Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wogns3623/eslint-plugin-better-exhaustive-deps

Eslint plugin for react hooks exhaustive-deps check with static value tracking
https://github.com/wogns3623/eslint-plugin-better-exhaustive-deps

eslint eslint-plugin exhaustive-deps react react-hooks

Last synced: 1 day ago
JSON representation

Eslint plugin for react hooks exhaustive-deps check with static value tracking

Awesome Lists containing this project

README

        

# Better react exhaustive-deps eslint plugin with static variable check

## Installation

```bash
yarn add -D @wogns3623/eslint-plugin-better-exhaustive-deps
```

or

```bash
pnpm i -D @wogns3623/eslint-plugin-better-exhaustive-deps
```

or

```bash
npm i -D @wogns3623/eslint-plugin-better-exhaustive-deps
```

## Usage

To use this plugin, you must disable original `react-hooks/exhaustive-deps` rule

```javascript
module.exports = {
// ...
plugins: ["@wogns3623/better-exhaustive-deps"],
rules: {
"react-hooks/exhaustive-deps": "off",
}
// ...
}
```

### checkMemoizedVariableIsStatic

Check variable memoized by `useCallback` or `useMemo` is static.

If Deps is empty or filled with static variable, the return value is also treated as a static variable.

```javascript
module.exports = {
// ...
plugins: ["@wogns3623/better-exhaustive-deps"],
rules: {
// disable original rule
"react-hooks/exhaustive-deps": "off",
"@wogns3623/better-exhaustive-deps/exhaustive-deps": [
"warn",
{
"checkMemoizedVariableIsStatic": true,
},
],
},
// ...
};

```

### staticHooks

Return values ​​registered as true are treated as static variable.

Can configure destructured value independently.

```javascript
module.exports = {
// ...
plugins: ["@wogns3623/better-exhaustive-deps"],
rules: {
// disable original rule
"react-hooks/exhaustive-deps": "off",
"@wogns3623/better-exhaustive-deps/exhaustive-deps": [
"warn",
{
"staticHooks": {
"useCustomRef": true,
"useSomething": [false, true],
"useSomethingOther": {
"value": false,
"callback": true
},
},
},
],
},
// ...
};
```

> use [grncdr](https://github.com/grncdr)'s code
> [https://github.com/facebook/react/issues/16873#issuecomment-536346885](https://github.com/facebook/react/issues/16873#issuecomment-536346885)

## TODO

- [X] Check callback generated by useCallback is immutable
- [ ] Create a rule to check for unnecessary static (stable) values in dependency list.
- [ ] Add presets for popular React libraries
- [ ] Add automatic inference for whether a custom hook return value is static or not