-
Notifications
You must be signed in to change notification settings - Fork 46k
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 - Custom hook in a typescript class method trigger an error about class component #26731
Comments
@nbrignol Why are you trying to write a hook as a class? There's no reason to. |
@nbrignol you are returning react hooks from the class based component that in itself breaking the reack hook rules. Let us know your usecase we will try to help you with that. |
Because I want to :p. Class methods are functions, right ? Actually this is working very well. The problems comes from the linter's message that is not right. |
@nbrignol there is genuinely no good reason to do this that I can think of. There's no benefit. A hook doesn't need to be "abstracted behind an interface". It shouldn't have access to class field methods. It needs to be based solely on the inputs and calling other hooks. You're running into a problem you've created for yourself :) Just write it as a normal standalone function and move on. |
Again I think this is not the point. Either it's a technical limitation and can't do that : I can understand, and I think you should add a dedicated message because this one does not match the context; In both case, ESLint doesn't handle well this situation, that's why I am filling this "bug". Thanks for your time. |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you! |
When I create a custom hook in a simple Typescript class, ESLINT is giving me this error :
React Hook "useState" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks
However, this is not an expected error because I am not working with a class component.
It should not trigger any error OR at least trigger a specific, adapted error if anything.
Standard implementation (classic function based) for reference :
This works well, my output is :
Class+method based implementation :
This implementation works but trigger the unexpected error (React Hook "useState" cannot be called in a class component).
Output :
Expected Behavior
I expect no warning/error when implementing a custom hook in a method OR a specific error targeting custom hook in simple class method if any reason to disallow that.
Note
This error is not limited to "useState". Any hook used is concerned (useEffect, useMutation for React Query, etc.)
The text was updated successfully, but these errors were encountered: