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

react-hoosk/rules-of-hooks suggestion: Don't consider "throw new Error" as a suspend #14343

Closed
Jessidhia opened this issue Nov 28, 2018 · 1 comment

Comments

@Jessidhia
Copy link
Contributor

Do you want to request a feature or report a bug? feature

What is the current behavior?

Currently, if a component uses the throw keyword and still has hooks declared after it, the lint rule warns that it is an incorrect use. However, it may be a false positive when throw is being used to assert a component is being used correctly.

export function ComponentUsingReduxContext() {
  const redux = React.useContext(ReactReduxContext)
  if (redux === null) {
    throw new Error('ComponentUsingReduxContext mounted outside a redux Provider')
  }
  // more validation, initializing, etc
  // ...
  // causes a lint warning
  React.useEffect(() => {})

  return null
}

What is the expected behavior?

throw new Error, or at least one where the Identifier's name .endsWith('Error'), could be considered an exception to the "no early returns" rule as the component will never try to reuse the state from the current render.

Currently, when you suspend by throwing a Promise, you also lose your state, so arguably all throws could be ignored, but I see that more as a bug with suspending than intended behavior.

@sophiebits
Copy link
Collaborator

I fixed this in #14040.

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

No branches or pull requests

2 participants