-
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
false positive with for
loop in function body
#31900
Comments
Sometimes, ESLint misinterprets the structure of the code, particularly when using modern JavaScript/TypeScript features. |
Hi @imjordanxd , |
After some research, it appears that ESLint detects an error when using a classic loop (for, while and do while) in a component that contains hook (useState, useEffect...) To overcome this problem, you can either :
The code would then look like this: const Dots = () => {
const count = 9;
const [highlightIndex, updateHighlightIndex] = React.useState(0);
React.useEffect(() => {
const updateHighlightIndexIntervalID = setInterval(() => {
updateHighlightIndex((i) => (i + 1) % count);
}, 200);
return () => {
clearInterval(updateHighlightIndexIntervalID);
};
}, []);
const dots = [...Array(count)].map((_, i) => (
<span key={i} style={{ opacity: i === highlightIndex ? 1 : 0.5 }}>
{i}
</span>
));
return <div>{dots}</div>;
}; I don't know if this is the behavior expected by |
Looks like a duplicate of #31687 |
Oh my bad, you're right! |
React version: N/A
Eslint-plugin-react-hooks version: 5.1.0
Steps To Reproduce
Link to code example:
The current behavior
The linter reports the following error:
This is incorrect. The for loop is correctly reading a reactive variable. No hooks are called conditionally or inside a loop. The code can be rewritten to satisfy the linter but there is nothing wrong with the original code.
The expected behavior
No error is reported. Having a
for
loop reading a reactive variable should not report an error.The text was updated successfully, but these errors were encountered: