Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
eslint-plugin-react-hooks-addons
Advanced tools
ESLint plugin that finds unused dependencies in React Hooks.
ESLint plugin that finds unused dependencies in React Hooks.
eslint-plugin-react-hooks is awesome for linting the dependency array of React Hooks. However, it doesn't do one thing: unused dependencies in the useEffect
or useLayoutEffect
hooks are not reported. Variables, which are included in useEffect
's dependency array but not used in its function scope, are perfectly valid in certain use cases. But it might also be a programmatic error which causes the effect hook to run unintentionally.
Take the following code as an example:
const [user1, setUser1] = useState();
const [user2, setUser2] = useState();
useEffect(() => {
fetch(`someUrl/${user1}`).then(/* ... */);
fetch(`someUrl/${user2}`).then(/* ... */);
}, [user1, user2]);
Next day you update the code and remove the second fetch
but forget to remove user2
from the dependency array:
const [user1, setUser1] = useState();
const [user2, setUser2] = useState();
useEffect(() => {
fetch(`someUrl/${user1}`).then(/* ... */);
}, [user1, user2]);
Then the useEffect
will run whenever user1
or user2
changes, which is probably not your intention. Similar errors occur more frequently when the hook updater function is large and there is a long dependency array. This eslint plugin checks and reports this kind of error.
What if I have a value which is not used in the hook function scope but I want the effect hook to run whenever that value has changed?
You could prepend a /* effect dep */
comment to the value in dependency array then it will be skipped during linting. It brings an addition benefit: the value is explicitly marked as effectful so that other people coming across the code will understand it's not a programmatic error.
useEffect(() => {
fetch(`someUrl/${user1}`).then(/* ... */);
- }, [user1, user2]);
+ }, [user1, /* effect dep */ user2]);
With npm
npm install -D eslint-plugin-react-hooks-addons
Or with Yarn
yarn add -D eslint-plugin-react-hooks-addons
In your ESLint configuration file:
{
"plugins": ["react-hooks-addons"],
"rules": {
"react-hooks-addons/no-unused-deps": "warn"
}
}
Explicitly mark a dependency as effectful with /* effect dep */
comment:
useEffect(() => {
// ...
}, [unusedVar, /* effect dep */ effectVar]);
Then only the unusedVar
will be reported as an unused dependency.
Note: this eslint plugin is supposed to work in tandem with eslint-plugin-react-hooks, as it doesn't check things that have already been reported by that plugin.
MIT Licensed.
FAQs
ESLint rule to check unused and potentially unnecessary dependencies in React Hooks.
The npm package eslint-plugin-react-hooks-addons receives a total of 10,551 weekly downloads. As such, eslint-plugin-react-hooks-addons popularity was classified as popular.
We found that eslint-plugin-react-hooks-addons demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.