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.

[eslint-plugin-react-hooks] Bug: exhaustive-deps not working with function declaration

See original GitHub issue

When extracting a useEffect callback from inline to a funcion declaration, the exhaustive-deps rule stops showing warnings related to the referenced function

React version: 16.12.0 eslint-plugin-react-hooks version: 1.7.0

Steps To Reproduce

  1. Add a basic useEffect code which doesn’t declare one of the dependencies in the array (tmp)
  const [tmp, setTmp] = useState("");

  useEffect(() => {
    console.log(tmp);
  }, []);
  1. ESLint shows the exhaustive-deps warning
  2. Extract the useEffect callback to a function declaration
  const [tmp, setTmp] = useState("");

  function sideEffect() {
    console.log(tmp);
  }

  useEffect(sideEffect, []);

Link to code example: https://codesandbox.io/s/summer-sunset-oie9s

The current behavior

ESLint plugin does not show the exhaustive-deps warning

The expected behavior

ESLint plugin should work even if the useEffect callback isn’t inline

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Mar 3, 2020
1reaction
yuriybelikecommented, Feb 29, 2020

Sorry for deleting the previous comment, wrong account.

The effects weren’t meant to be reused, the goal was to name them, in order to avoid having to read the impl to understand what they do.

I preffered this approach over custom hooks because the code is coupled to the component and is not really reusable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Definition for rule 'react-hooks/exhaustive-deps' was not found
I referred to this post to fix this but the solution mentioned doesn't work in my case.
Read more >
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 >
Hooks FAQ - React
We provide an ESLint plugin that enforces rules of Hooks to avoid bugs. It assumes that any function starting with ” use ”...
Read more >
How to manage the useEffect dependency array like a pro?
But we still have some work to have exhaustive dependencies! Declare the function inside the useEffect. If we look at the eslint warning...
Read more >
Best Practices with React Hooks - Bits and Pieces
Previously, React features such as state and lifecycle functions are available only for class-based components. Function-based components ...
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