-
Notifications
You must be signed in to change notification settings - Fork 49.3k
Closed
Labels
Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugA potential issue that we haven't yet confirmed as a bug
Description
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
- Create a functional component with
useState
hook. - Add a
for
loop inside the component (but outside the hook). - 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.
Mathias-S, jonkoops, shanimal08, MattCrouch, skratchdot and 23 more
Metadata
Metadata
Assignees
Labels
Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugA potential issue that we haven't yet confirmed as a bug