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 misses a case with useCallback

See original GitHub issue

The following causes an eslint warning (as it should)

const callback = useCallback(()=> cb(), [])

React Hook useCallback has a missing dependency: 'cb'. Either include it or remove the dependency array. If 'cb' changes too often, find the parent component that defines it and wrap that definition in useCallback. (react-hooks/exhaustive-deps)eslint

However, the following does not cause the warning

const callback = useCallback(cb, [])

but it should because this line is exposed to the same stale closure bug as the first.

(Maybe this is an antipattern, but in any case, the linter should be able to notice the problem, because this pattern can be used to “get around” the warning)

React version: 16.12.0

Link to code example: Codesandbox

The current behavior

The linter only notices that the function is missing from the deps array if it is in the form useCallback(() => cb(), [])

The expected behavior

The linter should notice that the function is missing from the deps array in the following form: useCallback(cb, [])

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
vkurchatkincommented, Feb 19, 2020

This is definitely an antipattern. Let’s say linter would make you write this:

const callback =  useCallback(() => cb(), [cb])

This code is absolute meaningless as it’s essentially the same as const callback = cb.

0reactions
gaearoncommented, May 26, 2020

Pretty sure this has been fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix missing dependency warning when using useEffect ...
So there are multiple React methods in hooks that are considered stable and non-exhausted where you do not have to apply to the...
Read more >
Hooks API Reference - React
If your update function returns the exact same value as the current state, the subsequent rerender will be skipped completely. Note. Unlike the...
Read more >
Understanding the exhaustive-deps Eslint rule in React
The "react-hooks/exhaustive-deps" rule warns us when we have a missing dependency in an effect hook. To get rid of the warning, move the...
Read more >
How to manage the useEffect dependency array like a pro?
So we fetch all the results at once. But react-hooks/exhaustive-deps rule raises a warning : React Hook useEffect has a missing dependency: ' ......
Read more >
React.useEffect Hook – Common Problems and How to Fix ...
React Hook useEffect has a missing dependency: 'user'. Either include it or remove the dependency array. (react-hooks/exhaustive-deps).
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