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] react-hooks/exhaustive-deps throws lint error `Pass an inline function` for valid scenarios

See original GitHub issue

Running lint rule react-hooks/exhaustive-deps Throws an error for cases where we use lodash methods such _throttle or debounce inside useCallback . Refactoring them would make useCallback becomes useless as the cache will be invalidated every time.

error  React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead  react-hooks/exhaustive-deps

React version: 16.13.1 eslint-plugin-react-hooks version 4.0.0

Steps To Reproduce

  1. Upgrade eslint-plugin-react-hooks to version 4.0.0
  2. Add lint rule to your eslinerc file ‘react-hooks/exhaustive-deps’: ‘error’,
  3. Run it on the following snippet.

code example:

     const throttledMethod = React.useCallback(
        _.throttle(abc, 500 ),
        [abc],
    );

The current behavior

Throws error because it expects an inline function

error  React Hook useCallback received a function whose dependencies are unknown. Pass an inline function instead  react-hooks/exhaustive-deps

The expected behavior

This is a valid scenario, should not throw error as if you refactor it, useCallback becomes useless as the cache will be invalidated every time.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:16
  • Comments:6

github_iconTop GitHub Comments

237reactions
vkurchatkincommented, Jul 2, 2020

useCallback is specifically designed for inline functions. For cases like this you need to use useMemo:

 const throttledMethod = React.useMemo(
        () => _.throttle(abc, 500 ),
        [abc],
    );
8reactions
XiaoJiuDYScommented, Jun 2, 2021

This will not report an error, what is the difference about useMemo and useCallBack in this scenario?

 const throttledMethod = React.useCallBack(
        () => _.throttle(abc, 500 ),
        [abc],
    );
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix missing dependency warning when using useEffect ...
So just pass the function directly to useEffect as in 1. ... It's not a JavaScript/React error, but an ESLint (eslint-plugin-react-hooks) warning.
Read more >
Subscription Initialization throws Error "The option "INLINE ...
1st Database: Message: The option "INLINE=ON" is not valid for this function. Check the documentation for the constructs supported with INLINE ...
Read more >
React v17.0 Release Candidate: No New Features
In React components, you usually write event handlers inline: ... because our eslint-plugin-react-hooks/exhaustive-deps lint rule (make sure ...
Read more >
Inline functions | Kotlin Documentation
The inline modifier affects both the function itself and the lambdas passed to it: all of those will be inlined into the call...
Read more >
PRE00-C. Prefer inline or static functions to function-like macros
Noncompliant Code Example. In this noncompliant code example, the macro CUBE() has undefined behavior when passed an expression that contains side effects: # ......
Read more >

github_iconTop Related Medium Post

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