question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: react-hooks/exhaustive-deps not warning about unneeded dep

See original GitHub issue

In 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

  1. Open Codesanbox.
  2. 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:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
awearycommented, Apr 13, 2020

@tonix-tuft exactly, useLayoutEffect is just like useEffect. The only difference is the timing.

1reaction
awearycommented, Apr 13, 2020

@tonix-tuft yes, useEffect is different in this respect. The purpose of useCallback and useMemo 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 changes

let [value, setValue] = useState("");
let [errors, setErrors] = useState(null);

// The input's value changed, clear any errors we reported earlier!
useEffect(() => {
  setErrors(null);
}, [input])

Hope that clears things up!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found