Skip to content
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

[eslint-plugin-react-hooks] Apply the rules of hooks to a forwardRef-wrapped component #17220

Closed
dprgarner opened this issue Oct 30, 2019 · 0 comments · Fixed by #17255
Closed

Comments

@dprgarner
Copy link
Contributor

dprgarner commented Oct 30, 2019

Do you want to request a feature or report a bug?
Feature (to catch potential bugs)

What is the current behavior?
The react-hooks/rules-of-hooks ESLint rule catches uses of hooks in conditionals in components, but does not recognise an anonymous function wrapped in forwardRef as a component.

The following example breaks the rules of hooks, but isn't caught by the rule:

// This should fail
const FancyButton = React.forwardRef((props, ref) => {
  if (props.fancy) {
    useCustomHook();
  }
  return <button ref={ref}>{props.children}</button>;
});

What is the expected behavior?
The above example should be caught by react-hooks/rules-of-hooks, and raise the "React Hook "useCustomHook" is called conditionally" error.

dprgarner added a commit to dprgarner/react that referenced this issue Nov 2, 2019
dprgarner added a commit to dprgarner/react that referenced this issue Nov 2, 2019
dprgarner added a commit to dprgarner/react that referenced this issue Nov 9, 2019
gaearon pushed a commit that referenced this issue Nov 17, 2019
* [eslint] Check forwardRef callbacks (#17220)

* [eslint] Make tests more realistic (#17220)

* [eslint] Check anonymous callback of React.memo for rules-of-hooks (#17220)

* [eslint] Add tests for callbacks not known to be components (#17220)

* [eslint] Correct comments and add another test (#17220)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants