@@ -2,20 +2,21 @@ import { useContext, useMemo, useState } from "react";
2
2
import { assertErrorBoundaryContext } from "./assertErrorBoundaryContext" ;
3
3
import { ErrorBoundaryContext } from "./ErrorBoundaryContext" ;
4
4
5
- export type UseErrorBoundaryApi < Error > = {
5
+ type UseErrorBoundaryState < TError > =
6
+ | { error : TError ; hasError : true }
7
+ | { error : null ; hasError : false } ;
8
+
9
+ export type UseErrorBoundaryApi < TError > = {
6
10
resetBoundary : ( ) => void ;
7
- showBoundary : ( error : Error ) => void ;
11
+ showBoundary : ( error : TError ) => void ;
8
12
} ;
9
13
10
- export function useErrorBoundary < Error = any > ( ) : UseErrorBoundaryApi < Error > {
14
+ export function useErrorBoundary < TError = any > ( ) : UseErrorBoundaryApi < TError > {
11
15
const context = useContext ( ErrorBoundaryContext ) ;
12
16
13
17
assertErrorBoundaryContext ( context ) ;
14
18
15
- const [ state , setState ] = useState < {
16
- error : Error | null ;
17
- hasError : boolean ;
18
- } > ( {
19
+ const [ state , setState ] = useState < UseErrorBoundaryState < TError > > ( {
19
20
error : null ,
20
21
hasError : false ,
21
22
} ) ;
@@ -26,7 +27,7 @@ export function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error> {
26
27
context ?. resetErrorBoundary ( ) ;
27
28
setState ( { error : null , hasError : false } ) ;
28
29
} ,
29
- showBoundary : ( error : Error ) =>
30
+ showBoundary : ( error : TError ) =>
30
31
setState ( {
31
32
error,
32
33
hasError : true ,
0 commit comments