-
Notifications
You must be signed in to change notification settings - Fork 46.4k
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
[ESLint] 'exhaustive-deps' lint rule - false warning and incorrect correction #16001
Comments
I reworked it using refs. Is this the "proper" way?
|
Also, if I create a custom hook, it no longer warns me about memoizing the onClick function. Is that a bug or is that on purpose... I don't see the purpose of memoizing the callback for the current or previous code sample since it only runs once.
|
Yeah.. That's not how this rule is supposed to be used. I wish there was a way to mark ESLint autofix suggestions as manual — so that you can still autofill them from an IDE but they would require a manual review. For this rule, it's supposed that you always read the message. See #15204 (comment) for past discussions. The rule points out correct problems, but the fix isn't always what it suggests (because sometimes you need to restructure your code). I think it's possible we'll hide autofix behind an option in the future. Or, ideally, work with ESLint to add a way to flag autofix suggestions as IDE-only. This might help understand it in the meantime: |
Here's a fixed version: https://codesandbox.io/s/multiple-alert-countdown-xrfwq. Since the function is passed down and is used in an effect, I wrap it in |
The reason the lint rule complaints is because
It's on purpose because you're not breaking the rules anymore. With your solution you always get the latest One downside is that the ref solution wouldn't let your effect "react" to changes in I'll close this issue since core complaint is same as #15204. Hope this helps. I hope FAQ resources above are also helpful. (And if you want a real deep dive, I wrote about it here and here.) |
I really appreciate the response. I considered useCallback and useMemo as performance optimizations for computationally expensive items rather than referencing functions / variables. I'll reevaluate my mindset for React hooks. |
Regarding the autofix, I hope eslint/rfcs#30 will let us solve the problem without compromising on IDE suggestions. |
Let's consolidate and track this in #16313. |
This is resolved now. |
Do you want to request a feature or report a bug?
I have eslint autofix my errors. For the first time, it caused a bug rather than helped.
Eslint will auto add dependencies that cause the code to improperly function. In my example, it added onClick. Then it would request me to change onClick to a useCallback (FYI, I did not show the useCallback changes). If I did make those changes, it would disrupt the order the test buttons disappear.
What is the current behavior?
CodeSandbox link: https://codesandbox.io/s/multiple-alert-countdown-294lc
What is the expected behavior?
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
react 16.8.6
eslint-plugin-react-hooks 1.6.0
The text was updated successfully, but these errors were encountered: