Skip to content

Bug: rules-of-hooks violation not caught by eslint plugin #22520

Closed as not planned

Description

@pke

Given the following code:

const createHandler = () => (event) => {
  const { t } = useTranslation()

  Alert.alert(t("title"), t("message"))
}

function MyComponent() {
  return <Button onPress={createHandler()}/>
}

the rules-of-hooks linter rule is not triggered for the createHandler as it should.
The linter rule should detect its not a react component, since it does not return any ReactElement or null.

Activity

  1. added
    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bug
    on Oct 6, 2021
  2. stale commented on Jan 8, 2022

    @stale

    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!

  3. dcheng1290 commented on Feb 22, 2022

    @dcheng1290

    @pke did you resolve this issue by any chance?

  4. pke commented on Feb 22, 2022

    @pke
    Author

    No

  5. eps1lon commented on Feb 23, 2022

    @eps1lon
    Collaborator

    This may have been an intentional case that's missed so existing code isn't falsely flagged. Assuming that any use* function is a hook, the plugin would trigger some false-positives in the ecosystem. So the plugin is probably assuming here that useTranslate is not a hook because it's neither called in another hook nor directly during render. Tracking createHandler usage would be really hard to accomplish.

    Consider e.g. useFakeTimers in sinon. If this would be considered a hook by the plugin, people would probably disable it due to the noise it creates.

    Maybe @gaearon can confirm this is intentional.

    Does the plugin flag usage of built-in hooks inside createHandler?

  6. dcheng1290 commented on Feb 23, 2022

    @dcheng1290

    Makes sense regarding existing code getting falsely flagged. I was wondering if the plugin currently has a rule for us to enable in order to capture this case. Thanks!

  7. eduardodallmann commented on Jan 14, 2023

    @eduardodallmann

    I simply import like this: import { useTranslation as translation } from 'react-i18next';

  8. pke commented on Jan 19, 2023

    @pke
    Author

    How is that helping solving the bug shown in this issue?

  9. eduardodallmann commented on Jan 20, 2023

    @eduardodallmann

    Sorry, I understood the problem to be an eslint warning in the "react-hooks/rules-of-hooks" rule for using a use* hook outside of a react component. Then I tricked eslint by renaming the import. My mistake.

  10. added a commit that references this issue on Feb 3, 2024
  11. github-actions commented on Apr 10, 2024

    @github-actions

    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!

  12. github-actions commented on Apr 17, 2024

    @github-actions

    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!

  13. scamden commented on Apr 26, 2024

    @scamden

    this was closed but this is still definitely an issue:

    for example when using jotai it's really easy for a dev to accidentally think they can use a hook and it's not caught:

    export const atomBiSavedViewExamples = atom(
      async (get) => {
       // no error here..
        const showWatershedSavedViewTemplates = useFeatureFlag(
          'myFlag'
        );
       //...etc
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions