Fix: Prevent false positives in rules-of-hooks for regular classesFix: Prevent false positives in rules-of-hooks for regular classes #35265
+104
−41
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?