-
Notifications
You must be signed in to change notification settings - Fork 50.2k
Fix: Prevent false positives in rules-of-hooks for regular classesFix: Prevent false positives in rules-of-hooks for regular classes #35265
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
base: main
Are you sure you want to change the base?
Conversation
The ESLint rule 'react-hooks/rules-of-hooks' was incorrectly flagging hooks used in regular class methods as invalid, assuming all classes are React components. This fix adds a helper function isReactComponentClass() that checks if a class actually extends React.Component or React.PureComponent before reporting an error. Regular classes (without extends) can now use hooks in their methods without triggering false positives. Fixes facebook#35264
|
Hi @hukshh! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
🔬 ULTRA-MICROSCOPIC DEEP DIVE: Rules of Hooks False Positive FixThis is an exceptionally important fix that addresses a fundamental flaw in the ESLint rules-of-hooks implementation. Let me provide an exhaustive, line-by-line analysis. 🎯 THE CORE PROBLEM: Semantic Ambiguity in JavaScript ClassesThe Bug in Plain English:The current implementation incorrectly assumes that ANY method in ANY class that starts with "use" is a React Hook being called in a class component. This creates false positives for:
Why This Matters:// ❌ INCORRECTLY FLAGGED AS ERROR (before this fix)
class Store {
use() {
return React.useState(4); // This is FINE - not a React component!
}
}
// ✅ CORRECTLY FLAGGED AS ERROR (should remain)
class MyComponent extends React.Component {
componentDidMount() {
useState(0); // This is WRONG - hooks in class components
}
}The distinction is critical:
🔧 THE SOLUTION: Semantic Class DetectionNew Function:
|
💡 SOLUTION: Complete Implementation with All Edge CasesBased on my ultra-deep analysis, here's a production-ready solution that addresses all identified issues: 🔧 IMPROVED IMPLEMENTATION1. Enhanced
|
Description
This PR fixes #35264 by preventing false positives in the
react-hooks/rules-of-hooksESLint rule when hooks are used inside regular class instances (not React components).Problem
The ESLint rule was incorrectly flagging hooks used in ANY class method as invalid, assuming all classes are React components. This caused false positives for regular classes that don't extend React.Component.
Example of false positive:
Solution
Added a helper function
isReactComponentClass()that checks if a class actually extendsReact.ComponentorReact.PureComponentbefore reporting an error. Regular classes (without extends or extending other classes) can now use hooks in their methods without triggering false positives.Changes
isReactComponentClass()helper function inRulesOfHooks.tsTesting
This fix adds a helper function isReactComponentClass() that checks if a class actually extends React.Component or React.PureComponent before reporting an error. Regular classes (without extends) can now use hooks in their methods without triggering false positives.
Fixes #35264
Summary
How did you test this change?