-
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
Bug: [eslint-plugin-react-hooks] incorrectly reports an error when hook is called outside of a loop. #31687
Comments
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 |
Similarly (but having react 18), having a |
I've added the following code to the {
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: 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: |
Thanks for the reports, folks. I think we need to handle |
Fix in #31720 |
Thanks @tyxla ! Since it was a regression, is it planned to release a patch version of eslint-plugin-react-hooks soon ? |
@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 |
While waiting for the fix to be published, downgrading to |
Will there be a patch release soon with this bug fix? |
The following code triggers an ESLint error with the rule
eslintreact-hooks/rules-of-hooks
stating: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
useState
hook.for
loop inside the component (but outside the hook).eslint-react-hooks
plugin enabled.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.The text was updated successfully, but these errors were encountered: