Bug: react-hooks/exhaustive-deps not warning about unneeded dep
See original GitHub issueIn the following example:
import ReactDOM from "react-dom";
import React, { useEffect } from "react";
import "./styles.css";
function Example() {
const id = 123;
const valueWhichMayChange = Math.random();
useEffect(() => {
return () => {
//valueWhichMayChange === 0.5 &&
document.getElementById(id).click(); // Just dummy functionality...
};
}, [id, valueWhichMayChange]); // Would expect unneeded "valueWhichMayChange" dep warning here.
return <div id="123" />;
}
ReactDOM.render(<Example />, document.getElementById("root"));
react-hooks/exhaustive-deps
doesn’t warn me about valueWhichMayChange
unneeded dep, even though I have commented the line:
...
//valueWhichMayChange === 0.5 &&
...
The same thing happens if I completely delete the line, no warning issued.
React version: 16.12
Steps To Reproduce
- Open Codesanbox.
- Look at line 13.
Link to code example: https://codesandbox.io/s/react-example-ey9pw?file=/index.js
The current behavior
No warning for valueWhichMayChange
, though it is an unneeded dependency.
The expected behavior
Warning for valueWhichMayChange
unneeded dependency.
Thanks a lot for your attention!
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Understanding the React exhaustive-deps linting warning
The first exhaustive deps warning we got was because a single primitive variable was missing in the dependency array. It is pretty simple...
Read more >react-hooks/exhaustive-deps warning--unclear about course ...
The issue here, and what the warning is pointing out, is that React hook callbacks are synchronous functions.
Read more >Why you shouldn't put refs in a dependency array - Epic React
Ah, we get a lint warning from the exhuastive-deps rule: React Hook React.useEffect has an unnecessary dependency: 'usernameInputRef.current'. Either exclude it ...
Read more >How To Fix "react hook useeffect has a missing dependency"?
An in-depth article on solving the "react hook useeffect has a missing dependency" warning. Why does this warning happen?
Read more >What are dependency arrays in React? - Devtrium
If not, you really should!). The rule react-hooks/exhaustive-deps warns you when you're doing something wrong with your dependency array. If you ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@tonix-tuft exactly,
useLayoutEffect
is just likeuseEffect
. The only difference is the timing.@tonix-tuft yes,
useEffect
is different in this respect. The purpose ofuseCallback
anduseMemo
is to memoize some value based on a known set of inputs. If you have an unnecessary dependency, that memoized value will be re-created more more often than necessary.As @vkurchatkin mentioned, this doesn’t apply to
useEffect
. It’s a valid use case to trigger an effect when some value changes, even if you don’t use that value. For example, you might want to clear some errors in a form when some input value changesHope that clears things up!