-
Notifications
You must be signed in to change notification settings - Fork 48.6k
Bug: react-hooks/exhaustive-deps
false positive with direct function calls on an object?
#21624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is because of the |
@nmain good point, and thanks for explaining that, but I'm now wondering the following:
|
I believe this is intentional. See it here mentioned as an aside in a comment: https://github.com/facebook/react/blob/master/packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js#L890 I agree that |
Nice find! I looks like that block of code is specifically targeting
Good point, I think it'd be great if rule could be improved so that it only applies the warning when |
Could this warning be one that is optionally enabled? It seems to me that it would be less confusing to users if they could opt in and out of the warning. This would also lend itself to a nice place to document the edge case and why it is a provided warning. |
+1, just ran into this and it's slightly confusing. |
Duplicate of #16265 |
eslint-plugin-react-hooks
version:4.2.0
When calling a function within
useEffect
where the function exists directly on an object and the function is included in the effect's dependency array, thereact-hooks/exhaustive-dep
ESLint rule is triggered. I'm wondering whether this is a false positive, or if there is actually a valid reason for this ESLint rule to be triggered in this scenario.Please let me know if this has already been answered/explained elsewhere; I tried searching for this problem and couldn't find anything about it!
It's also worth noting that I found this out when trying to call
react-query
useMutation
mutateAsync
within auseEffect
call. As the return value ofuseMutation
is not referentially stable (TanStack/query#1858), including the whole object in the dependency array isn't feasible, as it causes the effect to fire more than it should. However, as mentioned in that linked issue, the "functions / objects on the returned object are referentially stable", which means that includingx.mutateAsync
(wherex
is assigned the return value of theuseMutation
call) in the dependency array means that the effect fires only when it actually needs to/should do. However when trying to do so,react-hooks/exhaustive-dep
returns the warning mentioned.I understand that I could get around this warning by destructuring the
mutateAsync
off of the returned object before using it inuseEffect
, but that would be worse DX wise in my opinion, and I'd like to avoid it if possible.Steps To Reproduce
useEffect
where the function exists directly on an object and include the function in the effect's dependency array.react-hooks/exhaustive-dep
.Link to code example: https://codesandbox.io/s/bold-fast-smo6g?file=/src/App.js
The above example shows the following scenarios (to show that the behavior feels inconsistent and potentially wrong):
useEffect
where the function exists directly on an object (the case in question).useEffect
where the function did exist on an object, but was called from a different variable.useEffect
where the value exists directly on an object.The current behavior
The
react-hooks/exhaustive-dep
rule returns the following warning (in the case in the code example above):The expected behavior
No warning comes from
react-hooks/exhaustive-dep
, or an explanation is given by the rule as to why this is a problem?The text was updated successfully, but these errors were encountered: