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

Bug: eslint-plugin-react-hooks does not catch early returns in anonymous default exports #20408

Closed
dreyks opened this issue Dec 8, 2020 · 3 comments

Comments

@dreyks
Copy link

dreyks commented Dec 8, 2020

React version:
17.0.1

Steps To Reproduce

import { useEffect } from 'react'

export default () => {
  if (true) return false

  useEffect(() => {})

  return false
}
  1. run the linter

The current behavior

No errors

The expected behavior

error React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?

failing test-suite case

{
  code: `
    export default () => {
      if (cond) {
        useConditionalHook();
      }
    }
  `,
  errors: [conditionalError('useConditionalHook')],
},
@dreyks dreyks added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Dec 8, 2020
@julien51
Copy link

(painfuly) confirmed!

@eps1lon
Copy link
Collaborator

eps1lon commented Dec 23, 2020

Unfortunately, this is expected behavior. The plugin requires that you name your function components:

Calls to Hooks are either inside a PascalCase function (assumed to be a component) or another useSomething function (assumed to be a custom Hook).

-- https://reactjs.org/docs/hooks-faq.html#what-exactly-do-the-lint-rules-enforce

@eps1lon eps1lon closed this as completed Dec 23, 2020
@eps1lon eps1lon added Resolution: Expected Behavior and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Dec 23, 2020
@dreyks
Copy link
Author

dreyks commented Dec 23, 2020

I see. sounds fair, thanks for the explanation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants