Bug: react-hooks/exhaustive-deps misses a case with useCallback
See original GitHub issueThe 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:
- Created 4 years ago
- Comments:7 (1 by maintainers)
This is definitely an antipattern. Let’s say linter would make you write this:
This code is absolute meaningless as it’s essentially the same as
const callback = cb
.Pretty sure this has been fixed.