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: [ESLint Hooks Plugin] false positive with useCallback and not inline function body

See original GitHub issue

I just received eslint-plugin-react-hooks@4.0 through dependabot, and I am a little confused by the error and the explanation in this PR https://github.com/facebook/react/pull/18435. I might be wrong but I don’t think I should be getting a lint warning/error.


const useFoo = <R>(
  selector: (state: S) => R,
  dependencies: DependencyList = []
) => {
  const memoizedSelector = useCallback(selector, dependencies)
  // React Hook useCallback has a missing dependency: 'selector'. Either include it or remove the dependency array  react-hooks/exhaustive-deps
  ...
}

My my selector function changes every time, and adding it to the dependencies list defeats the whole purpose of useCallback in the first place. Maybe I am missing something here.

I tried to create a codesandbox, but apparently custom eslint configs are not supported. Let me know what I can doo to help.

This is the relevant code: https://github.com/kilianc/mozzarella/blob/master/src/create-store.ts#L45-L70

This is the PR that complaints: https://github.com/kilianc/mozzarella/pull/8

This is the error from github actions: https://github.com/kilianc/mozzarella/runs/644435406?check_suite_focus=true

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
gaearoncommented, May 5, 2020

The purpose of the linter is to verify that you don’t have stale closures. If you pass invisible selector and dependencies we can’t verify that. You can add an ignore there if you’re convinced the deps are correct, but the warning itself is right — it says something may be unsafe. E.g. in your example, nothing will actually verify that useSelector receives correct deps.

0reactions
kilianccommented, May 6, 2020

To be more precise, I understand how

function Button({ onPressChange, isPressed }) {
  useEffect(onPressChange, [isPressed]);
}

could trigger a warning considering the inability to statically analyze onPressChange and the relation with isPressed.

What I am referring to here, given this example:

const useUseCallback = (fn, deps) => {
  return useCallback(fn, deps)
  // React Hook useCallback has a missing dependency: 'fn'. Either include it or remove the dependency array  react-hooks/exhaustive-deps
}
  • why am I a getting a warning at all
  • why is the warning telling me to add the function itself to the dependencies?

The warning is telling me there is a fix I need to apply to my code when In reality, given your feedback, the only path forward is disabling eslint for that line, am I correct?

Why is this code passing linting?

const useUseCallback = (fn, deps) => {
  return useCallback(fn, [...deps, fn])
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hook useCallback received a function whose ...
It seems the warning is there because useCallback (and also useMemo see below) expect inline function as argument (don't know why though).
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 >
eslint-plugin-react-hooks | Yarn - Package Manager
useInsertionEffect is a new hook that allows CSS-in-JS libraries to address performance issues of injecting styles in render. Unless you've already built a...
Read more >
The last guide to the useEffect Hook you'll ever need
Understanding how the useEffect Hook works, and why it requires a wholesale shift in mindset, is essential to writing modern React code.
Read more >
Hooks FAQ - React
We provide an ESLint plugin that enforces rules of Hooks to avoid bugs. ... render : This is the function component body itself....
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