Skip to content

Commit

Permalink
fix(useErrorHandler): use unknown instead of Error (#89)
Browse files Browse the repository at this point in the history
* fix(useErrorHandler): use unknown instead of Error type

* make the return type explicit
* treat null and undefined as no error

* docs(useErrorHandler): use unknown instead of error

Closes #83
  • Loading branch information
tommarien committed May 5, 2021
1 parent fb0b859 commit ec81d61
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -289,7 +289,7 @@ See the recovery examples above.
This is called when the `resetKeys` are changed (triggering a reset of the
`ErrorBoundary`). It's called with the `prevResetKeys` and the `resetKeys`.

### `useErrorHandler(error?: Error)`
### `useErrorHandler(error?: unknown)`

React's error boundaries feature is limited in that the boundaries can only
handle errors thrown during React's lifecycles. To quote
Expand Down
10 changes: 4 additions & 6 deletions src/index.tsx
Expand Up @@ -165,12 +165,10 @@ function withErrorBoundary<P>(
return Wrapped
}

function useErrorHandler<P = Error>(
givenError?: P | null | undefined,
): React.Dispatch<React.SetStateAction<P | null>> {
const [error, setError] = React.useState<P | null>(null)
if (givenError) throw givenError
if (error) throw error
function useErrorHandler(givenError?: unknown): (error: unknown) => void {
const [error, setError] = React.useState<unknown>(null)
if (givenError != null) throw givenError
if (error != null) throw error
return setError
}

Expand Down

0 comments on commit ec81d61

Please sign in to comment.