ESLint Override Comments: When and How
You'll learn the proper syntax for disabling the rules-of-hooks ESLint rule and when it's justified versus when you're masking a real problem.
The Nuclear Option
Sometimes ESLint's rules-of-hooks will flag code that you know is correct—or you need to temporarily bypass it. You can use special comments to disable the rule:
// eslint-disable-next-line react-hooks/rules-of-hooks
This tells ESLint to skip the next line. You can also disable for an entire file:
/* eslint-disable react-hooks/rules-of-hooks */
When It's Justified
Almost never. But rare legitimate cases include:
- Testing edge cases in a controlled test environment
- Code generation tools that produce valid hook code ESLint can't analyze
- Legacy migration during a refactor (temporarily, with a TODO comment)
When You're Hiding a Bug
Most of the time, if ESLint complains, you have a real violation:
- Calling hooks conditionally
- Calling hooks in loops
- Calling hooks in regular functions
- Calling hooks after early returns
Using eslint-disable doesn't fix these issues—it just hides the warning. The underlying bug remains, and React will behave unpredictably at runtime.
The Right Approach
Before reaching for the disable comment, ask: "Can I restructure this code to follow the rules?" The answer is almost always yes. Extract to a custom hook, move the conditional logic around the hook call, or rethink your component structure.
Key Takeaway: ESLint override comments for rules-of-hooks are a last resort—if you find yourself using them, it's usually a sign you need to refactor, not disable the warning.