[eslint-plugin-react-hooks] react-hooks/exhaustive-deps throws lint error `Pass an inline function` for valid scenarios
See original GitHub issueRunning 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
- Upgrade eslint-plugin-react-hooks to version 4.0.0
- Add lint rule to your eslinerc file ‘react-hooks/exhaustive-deps’: ‘error’,
- 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:
- Created 3 years ago
- Reactions:16
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
useCallback
is specifically designed for inline functions. For cases like this you need to useuseMemo
:This will not report an error, what is the difference about useMemo and useCallBack in this scenario?