-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
[react-hooks/exhaustive-deps] eslint --fix breaks the code #15204
Comments
Your code is buggy. Hook must be like that: import { useEffect, useState } from 'react'
export function useSwitchTab(trigger, tabsAmount, initialState = 0) {
const [currentTab, setTab] = useState(initialState - 1)
useEffect(() => {
setTab(tab => tab + 1 >= tabsAmount ? 0 : tab + 1)
}, [tabsAmount, trigger]) // eslint-disable-line react-hooks/exhaustive-deps
// also, trigger is unnecessary dependency by default so you need comment line in the top
return [currentTab, setTab]
} |
@infodusha , |
@EugeneDraitsev |
@infodusha Just imagine situation I have my version of code - it works fine, I pushed my changes to remote. CI executes 'eslint --fix' and deploy broken version of code to production (just because this is autofixable warning) Eslint --fix should never change logic of your code. But in my example eslint with this rule will change logic of my code. I pretty sure that this warning should be not-autofixable with --fix. |
@EugeneDraitsev I thought eslint-disabe shoud also disable |
The warning is not a false positive. You can see how to fix it here: https://reactjs.org/docs/hooks-faq.html#what-can-i-do-if-my-effect-dependencies-change-too-often In this particular case, the fix would be to function form of It's fair that there is an expectation that We could disable autofix by default, and add an option that enables it. I don't know if this is what most people want. |
@EugeneDraitsev
So, I think if you want to use |
@behzad888 I understand that my code is not correct from 'hooks best practice' point of view, but this code is still valid and will behave 'as designed'. Often peoples will create hooks, that will be not follow best practices or even documentation. As I said above and as @gaearon mentioned in 2nd part of his comment:
From my point of view make sense to change level of Of Course important to add some "suggestions" for user (and IDE), but maybe just an error description will be enough in this case. |
We ran into this issue... my team is new to hooks and we're making all kind of rookie mistakes ;) so when I turned on the new exhaustive deps rule and ran it through auto fix I had this unpleasant effect of code being utterly broken all over the place. So we have to disable the rule completely because people inadvertently run autofix and break code all over. We're going through and fixing all of these instances but yeah having "working" (yes I realize its broken in a sense) code all of a sudden cause fatal failures seems unacceptable for an "autofix". I don't think we'll ever enable this rule with autofix because we can't trust that it won't produce a fatally broken build. |
I have another case to add to this question. Suppose I have a hook like this:
If I use it in this way (is a simple example so excuse me about the logic, but suppose the useWindowPosition hook returns as the second element of the array a complex function):
Eslint fires the following:
What is the difference with use:
and why does eslint detect a possible dependency warning but when I use a |
@ger86 |
Here is my case : #16006 Disable autofix by default sounds good for me |
@gaearon - I arrived to this thread and similar ones, simply because eslint autofix for I've read threads on eslint regarding support for autofix disable for specific rules - Seems like there are no current solutions except some hacks you can do in your config + CI. The eslint guys emphasized several times - it is highly recommended that autofix should not alter runtime behavior, let alone break code. On one hand, we have a very important rule that we want to enforce. This autofix behavior leaves us with two non-optimal options:
I would rather have autofix for this completely disabled by default then being forced to choose one of the two options above. I believe most people will agree, based on the nature of this autofix. Is there any work being done on configuring autofix for this rule? I haven't seen any issue on this. Should I open one? |
Had a similar issue: The autofix then proceeded to wrap my function into useCallback()... without importing it, breaking my app. Autofix should really be disabled by default. |
@gaearon The autofix is still causing problems when adding this rule to a existing code base. Yes, there are potential problems in the code, which should be addressed, but this "fix" breaks the code (sometimes) immediately. |
The exhaustive-deps rule in combination with the autofix functionality of eslint (which we run on commit) can insert dependencies in our effects. While this is due to our code not following React best practices, our code works, and after the autofixing it may stop working. Discussion of the plugin shortcomings: facebook/react#15204 (comment)
I renamed this issue to avoid misunderstanding of it. Maybe we finally can get a decision about this problem and close it. |
Yes, this seriously needs to be addressed. I just added this lint rule to a large codebase and ran autofix to resolve some of the basic issues. My assumption was that the logic would not be changed, as is the reasonable expectation with ESLint fixes. However, after testing my code I discovered that it had been broken in tons of components. I get that there were technical issues in how the hooks were used, to begin with, which is exactly why there should have just been warning or error raised so that I had the opportunity to address them with the proper scrutiny. But having a potentially breaking fix is unreasonable and renders (what is otherwise a really good linting rule) rule dangerous. Please please please disable the autofix by default, or remove it entirely. In the meantime, I forsee this causing breaking changes in our codebases as logic is automagically changed during pre-commit hooks. |
This is resolved now. |
Sounds goods, I met the same problem |
Do you want to request a feature or report a bug?
I want to report a bug
What is the current behavior?
eslint --fix trying to break my hook)
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
I have a very simple hook:
If props
tabsAmount
ortrigger
will change, I need to increasecurrentTab
value by 1. It works fine and looks ok for me, but ineslint-plugin-react-hook
rulereact-hooks/exhaustive-deps
will warn here:And eslint --fix will break my code like this:
It will add
currentTab
in deps for useEffect and this will create endless loop.What is the expected behavior?
Eslint shouldn't fix this warning with --fix option, It may break the code.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
react: 16.8.5
eslint-plugin-react-hooks: 1.6.0
The text was updated successfully, but these errors were encountered: