question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[react-hooks/exhaustive-deps] eslint --fix breaks the code

See original GitHub issue

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:

import { useEffect, useState } from 'react'

export function useSwitchTab(trigger, tabsAmount, initialState = 0) {
  const [currentTab, setTab] = useState(initialState - 1)

  useEffect(() => {
    setTab(currentTab + 1 >= tabsAmount ? 0 : currentTab + 1)
  }, [tabsAmount, trigger])

  return [currentTab, setTab]
}

If props tabsAmount or trigger will change, I need to increase currentTab value by 1. It works fine and looks ok for me, but in eslint-plugin-react-hook rule react-hooks/exhaustive-deps will warn here:

React Hook useEffect has a missing dependency: ‘currentTab’. Either include it or remove the dependency array. You can also do a functional update ‘setTab(c => …)’ if you only need ‘c urrentTab’ in the ‘setTab’ call react-hooks/exhaustive-deps

And eslint --fix will break my code like this:

import { useEffect, useState } from 'react'

export function useSwitchTab(trigger, tabsAmount, initialState = 0) {
  const [currentTab, setTab] = useState(initialState - 1)

  useEffect(() => {
    setTab(currentTab + 1 >= tabsAmount ? 0 : currentTab + 1)
  }, [currentTab, tabsAmount, trigger])

  return [currentTab, setTab]
}

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:27
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

26reactions
EugeneDraitsevcommented, Mar 25, 2019

@infodusha I understand how eslint-disable works. Problem is in semantic of eslint --fix.

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.

26reactions
infodushacommented, Mar 25, 2019

@EugeneDraitsev // eslint-disable-line react-hooks/exhaustive-deps will help eslint --fix ignore this rule on line

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix a react-hooks/exhaustive-deps linting error with no ...
I understand how the code is breaking the rule, but I do not understand why this rule applies to this situation. I want...
Read more >
ESLint | IntelliJ IDEA Documentation - JetBrains
When you use the Reformat action, IntelliJ IDEA will then no longer break properly formatted code from the ESLint perspective. IntelliJ IDEA ...
Read more >
eslint-plugin-react-hooks - npm
This ESLint plugin enforces the Rules of Hooks. It is a part of the Hooks API for React.
Read more >
How to fix - react hook useEffect has missing dependencies?
get('/dummy-api', user); setUserDetails(res); <em>// eslint-disable-next-line react-hooks/exhaustive-deps</em> }, []); ... ... Code language: ...
Read more >
Rules of Hooks - React
By following this rule, you ensure that all stateful logic in a component is clearly visible from its source code. ESLint Plugin. We...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found