Description
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 throw
s could be ignored, but I see that more as a bug with suspending than intended behavior.