Skip to content

Commit 8c40f40

Browse files
committed
Merge branch 'master' of github.com:bvaughn/react-error-boundary
2 parents 9bbe30b + 1e75248 commit 8c40f40

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/ErrorBoundary.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ import { Component, createElement, ErrorInfo, isValidElement } from "react";
33
import { ErrorBoundaryContext } from "./ErrorBoundaryContext";
44
import { ErrorBoundaryProps, FallbackProps } from "./types";
55

6-
type ErrorBoundaryState = { didCatch: boolean; error: any };
6+
type ErrorBoundaryState =
7+
| {
8+
didCatch: true;
9+
error: any;
10+
}
11+
| {
12+
didCatch: false;
13+
error: null;
14+
};
715

816
const initialState: ErrorBoundaryState = {
917
didCatch: false,

src/useErrorBoundary.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ import { useContext, useMemo, useState } from "react";
22
import { assertErrorBoundaryContext } from "./assertErrorBoundaryContext";
33
import { ErrorBoundaryContext } from "./ErrorBoundaryContext";
44

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> = {
610
resetBoundary: () => void;
7-
showBoundary: (error: Error) => void;
11+
showBoundary: (error: TError) => void;
812
};
913

10-
export function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error> {
14+
export function useErrorBoundary<TError = any>(): UseErrorBoundaryApi<TError> {
1115
const context = useContext(ErrorBoundaryContext);
1216

1317
assertErrorBoundaryContext(context);
1418

15-
const [state, setState] = useState<{
16-
error: Error | null;
17-
hasError: boolean;
18-
}>({
19+
const [state, setState] = useState<UseErrorBoundaryState<TError>>({
1920
error: null,
2021
hasError: false,
2122
});
@@ -26,7 +27,7 @@ export function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error> {
2627
context?.resetErrorBoundary();
2728
setState({ error: null, hasError: false });
2829
},
29-
showBoundary: (error: Error) =>
30+
showBoundary: (error: TError) =>
3031
setState({
3132
error,
3233
hasError: true,

0 commit comments

Comments
 (0)