Skip to content

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

Closed
@Jessidhia

Description

@Jessidhia

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions