Bug: [ESLint Hooks Plugin] false positive with useCallback and not inline function body
See original GitHub issueI 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:
- Created 3 years ago
- Comments:7 (3 by maintainers)
The purpose of the linter is to verify that you don’t have stale closures. If you pass invisible
selector
anddependencies
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 thatuseSelector
receives correct deps.To be more precise, I understand how
could trigger a warning considering the inability to statically analyze
onPressChange
and the relation withisPressed
.What I am referring to here, given this example:
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?