Skip to content
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

Bug: [eslint-plugin-react-hooks] incorrectly reports an error when hook is called outside of a loop. #31687

Closed
akkadaska opened this issue Dec 6, 2024 · 9 comments · Fixed by #31720
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@akkadaska
Copy link

The following code triggers an ESLint error with the rule eslintreact-hooks/rules-of-hooks stating:

"React Hook 'useState' may be executed more than once. Possibly because it is called in a loop. React Hooks must be called in the exact same order in every component render."

However, the hook useState is not inside the loop, and there is no reason for the error to be thrown.

React version:

React 19.0.0 (latest stable)
eslint-plugin-react-hooks 5.1.0 (latest stable)

Steps To Reproduce

  1. Create a functional component with useState hook.
  2. Add a for loop inside the component (but outside the hook).
  3. Run the ESLint with the eslint-react-hooks plugin enabled.
const Component = () => {
  const [state, setState] = useState(0);

  for (let i = 0; i < 10; i++) {
    console.log(i);
  }

  return <div></div>;
};

Link to code example:

https://codesandbox.io/p/devbox/8dlj6f
Run npm run lint

The current behavior

ESLint throws an error about useState potentially being called multiple times, even though it is not in a loop.

The expected behavior

No ESLint error should be thrown, as useState is not inside the loop, and should follow the rule of hooks correctly.

@akkadaska akkadaska added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Dec 6, 2024
@jonkoops
Copy link

jonkoops commented Dec 6, 2024

We're seeing what appear to be false positives on our CI as well: https://github.com/keycloak/keycloak/actions/runs/12190763028/job/34008503162?pr=35684

@xobotyi
Copy link

xobotyi commented Dec 9, 2024

Similarly (but having react 18), having a for loop in the body of a functional component, even if it is unrelated to hooks, causes the linter to report that all hooks within that component with may be executed more than once error.

skratchdot added a commit to skratchdot/react that referenced this issue Dec 9, 2024
@skratchdot
Copy link
Contributor

I've added the following code to the valid array in packages/eslint-plugin-react-hooks/__tests__/ESLintRulesOfHooks-test.js:

    {
      code: normalizeIndent`
        // valid because the loop does not change the number of times the hook is called
        const Component = () => {
          const [state, setState] = useState(0);
          for (let i = 0; i < 10; i++) {
            console.log(i);
          }
          return <div></div>;
        };
      `,
    },

The test fails (but it should work). If I rollback this change:
https://github.com/facebook/react/pull/28714/files#diff-5649cc9bc31063c1139ad0f91e98add9c77f641281cba934123db8aeade0b97eL298

The test starts passing (but 8 other use-cases start failing). So we need to make some changes that get all the tests working I think.

I'll try to look in to the code and get this use-case working:
skratchdot@e4daf26

@tyxla
Copy link
Contributor

tyxla commented Dec 10, 2024

Thanks for the reports, folks.

I think we need to handle do/while a bit differently then. I have a few ideas from my past work on #28714, let me push a PR.

@tyxla
Copy link
Contributor

tyxla commented Dec 10, 2024

Fix in #31720

@xfournet
Copy link

Thanks @tyxla ! Since it was a regression, is it planned to release a patch version of eslint-plugin-react-hooks soon ?

@tyxla
Copy link
Contributor

tyxla commented Dec 10, 2024

@xfournet I'm not part of the React core team, so I can't tell, but let's summon some folks who were involved with #28714.

cc @mofeiZ and @josephsavona for review

@s100
Copy link

s100 commented Dec 12, 2024

While waiting for the fix to be published, downgrading to eslint-plugin-react-hooks@5.0.0 is a workaround.

@kaiyoma
Copy link

kaiyoma commented Dec 17, 2024

Will there be a patch release soon with this bug fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants