react-error-boundary 4.0.9 → 4.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/declarations/src/ErrorBoundary.d.ts +2 -2
- package/dist/declarations/src/types.d.ts +4 -6
- package/dist/react-error-boundary.cjs.js +17 -9
- package/dist/react-error-boundary.development.cjs.js +162 -0
- package/dist/react-error-boundary.development.cjs.mjs +6 -0
- package/dist/react-error-boundary.development.esm.js +155 -0
- package/dist/react-error-boundary.esm.js +17 -9
- package/package.json +16 -3
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Component, ErrorInfo
|
|
1
|
+
import { Component, ErrorInfo } from "react";
|
|
2
2
|
import { ErrorBoundaryProps } from "./types.js";
|
|
3
3
|
type ErrorBoundaryState = {
|
|
4
4
|
didCatch: boolean;
|
|
5
5
|
error: any;
|
|
6
6
|
};
|
|
7
|
-
export declare class ErrorBoundary extends Component<
|
|
7
|
+
export declare class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
8
8
|
constructor(props: ErrorBoundaryProps);
|
|
9
9
|
static getDerivedStateFromError(error: Error): {
|
|
10
10
|
didCatch: boolean;
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import { Component, ComponentType, FunctionComponent, ReactElement, ReactNode } from "react";
|
|
1
|
+
import { Component, ComponentType, ErrorInfo, FunctionComponent, PropsWithChildren, ReactElement, ReactNode } from "react";
|
|
2
2
|
declare function FallbackRender(props: FallbackProps): ReactNode;
|
|
3
3
|
export type FallbackProps = {
|
|
4
4
|
error: any;
|
|
5
5
|
resetErrorBoundary: (...args: any[]) => void;
|
|
6
6
|
};
|
|
7
|
-
type ErrorBoundarySharedProps = {
|
|
8
|
-
onError?: (error: Error, info:
|
|
9
|
-
componentStack: string;
|
|
10
|
-
}) => void;
|
|
7
|
+
type ErrorBoundarySharedProps = PropsWithChildren<{
|
|
8
|
+
onError?: (error: Error, info: ErrorInfo) => void;
|
|
11
9
|
onReset?: (details: {
|
|
12
10
|
reason: "imperative-api";
|
|
13
11
|
args: any[];
|
|
@@ -17,7 +15,7 @@ type ErrorBoundarySharedProps = {
|
|
|
17
15
|
next: any[] | undefined;
|
|
18
16
|
}) => void;
|
|
19
17
|
resetKeys?: any[];
|
|
20
|
-
}
|
|
18
|
+
}>;
|
|
21
19
|
export type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {
|
|
22
20
|
fallback?: never;
|
|
23
21
|
FallbackComponent: ComponentType<FallbackProps>;
|
|
@@ -23,12 +23,16 @@ class ErrorBoundary extends react.Component {
|
|
|
23
23
|
error
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
-
resetErrorBoundary(
|
|
26
|
+
resetErrorBoundary() {
|
|
27
27
|
const {
|
|
28
28
|
error
|
|
29
29
|
} = this.state;
|
|
30
30
|
if (error !== null) {
|
|
31
|
-
|
|
31
|
+
var _this$props$onReset, _this$props;
|
|
32
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
33
|
+
args[_key] = arguments[_key];
|
|
34
|
+
}
|
|
35
|
+
(_this$props$onReset = (_this$props = this.props).onReset) === null || _this$props$onReset === void 0 ? void 0 : _this$props$onReset.call(_this$props, {
|
|
32
36
|
args,
|
|
33
37
|
reason: "imperative-api"
|
|
34
38
|
});
|
|
@@ -36,7 +40,8 @@ class ErrorBoundary extends react.Component {
|
|
|
36
40
|
}
|
|
37
41
|
}
|
|
38
42
|
componentDidCatch(error, info) {
|
|
39
|
-
|
|
43
|
+
var _this$props$onError, _this$props2;
|
|
44
|
+
(_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 ? void 0 : _this$props$onError.call(_this$props2, error, info);
|
|
40
45
|
}
|
|
41
46
|
componentDidUpdate(prevProps, prevState) {
|
|
42
47
|
const {
|
|
@@ -52,7 +57,8 @@ class ErrorBoundary extends react.Component {
|
|
|
52
57
|
// So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.
|
|
53
58
|
|
|
54
59
|
if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
|
|
55
|
-
|
|
60
|
+
var _this$props$onReset2, _this$props3;
|
|
61
|
+
(_this$props$onReset2 = (_this$props3 = this.props).onReset) === null || _this$props$onReset2 === void 0 ? void 0 : _this$props$onReset2.call(_this$props3, {
|
|
56
62
|
next: resetKeys,
|
|
57
63
|
prev: prevProps.resetKeys,
|
|
58
64
|
reason: "keys"
|
|
@@ -84,7 +90,7 @@ class ErrorBoundary extends react.Component {
|
|
|
84
90
|
} else if (FallbackComponent) {
|
|
85
91
|
childToRender = react.createElement(FallbackComponent, props);
|
|
86
92
|
} else {
|
|
87
|
-
throw
|
|
93
|
+
throw error;
|
|
88
94
|
}
|
|
89
95
|
}
|
|
90
96
|
return react.createElement(ErrorBoundaryContext.Provider, {
|
|
@@ -96,7 +102,9 @@ class ErrorBoundary extends react.Component {
|
|
|
96
102
|
}, childToRender);
|
|
97
103
|
}
|
|
98
104
|
}
|
|
99
|
-
function hasArrayChanged(
|
|
105
|
+
function hasArrayChanged() {
|
|
106
|
+
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
107
|
+
let b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
100
108
|
return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
|
|
101
109
|
}
|
|
102
110
|
|
|
@@ -116,7 +124,7 @@ function useErrorBoundary() {
|
|
|
116
124
|
});
|
|
117
125
|
const memoized = react.useMemo(() => ({
|
|
118
126
|
resetBoundary: () => {
|
|
119
|
-
context
|
|
127
|
+
context === null || context === void 0 ? void 0 : context.resetErrorBoundary();
|
|
120
128
|
setState({
|
|
121
129
|
error: null,
|
|
122
130
|
hasError: false
|
|
@@ -126,7 +134,7 @@ function useErrorBoundary() {
|
|
|
126
134
|
error,
|
|
127
135
|
hasError: true
|
|
128
136
|
})
|
|
129
|
-
}), [context
|
|
137
|
+
}), [context === null || context === void 0 ? void 0 : context.resetErrorBoundary]);
|
|
130
138
|
if (state.hasError) {
|
|
131
139
|
throw state.error;
|
|
132
140
|
}
|
|
@@ -141,7 +149,7 @@ function withErrorBoundary(component, errorBoundaryProps) {
|
|
|
141
149
|
|
|
142
150
|
// Format for display in DevTools
|
|
143
151
|
const name = component.displayName || component.name || "Unknown";
|
|
144
|
-
Wrapped.displayName =
|
|
152
|
+
Wrapped.displayName = "withErrorBoundary(".concat(name, ")");
|
|
145
153
|
return Wrapped;
|
|
146
154
|
}
|
|
147
155
|
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var react = require('react');
|
|
7
|
+
|
|
8
|
+
const ErrorBoundaryContext = react.createContext(null);
|
|
9
|
+
|
|
10
|
+
const initialState = {
|
|
11
|
+
didCatch: false,
|
|
12
|
+
error: null
|
|
13
|
+
};
|
|
14
|
+
class ErrorBoundary extends react.Component {
|
|
15
|
+
constructor(props) {
|
|
16
|
+
super(props);
|
|
17
|
+
this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
|
|
18
|
+
this.state = initialState;
|
|
19
|
+
}
|
|
20
|
+
static getDerivedStateFromError(error) {
|
|
21
|
+
return {
|
|
22
|
+
didCatch: true,
|
|
23
|
+
error
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
resetErrorBoundary() {
|
|
27
|
+
const {
|
|
28
|
+
error
|
|
29
|
+
} = this.state;
|
|
30
|
+
if (error !== null) {
|
|
31
|
+
var _this$props$onReset, _this$props;
|
|
32
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
33
|
+
args[_key] = arguments[_key];
|
|
34
|
+
}
|
|
35
|
+
(_this$props$onReset = (_this$props = this.props).onReset) === null || _this$props$onReset === void 0 ? void 0 : _this$props$onReset.call(_this$props, {
|
|
36
|
+
args,
|
|
37
|
+
reason: "imperative-api"
|
|
38
|
+
});
|
|
39
|
+
this.setState(initialState);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
componentDidCatch(error, info) {
|
|
43
|
+
var _this$props$onError, _this$props2;
|
|
44
|
+
(_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 ? void 0 : _this$props$onError.call(_this$props2, error, info);
|
|
45
|
+
}
|
|
46
|
+
componentDidUpdate(prevProps, prevState) {
|
|
47
|
+
const {
|
|
48
|
+
didCatch
|
|
49
|
+
} = this.state;
|
|
50
|
+
const {
|
|
51
|
+
resetKeys
|
|
52
|
+
} = this.props;
|
|
53
|
+
|
|
54
|
+
// There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array,
|
|
55
|
+
// we'd end up resetting the error boundary immediately.
|
|
56
|
+
// This would likely trigger a second error to be thrown.
|
|
57
|
+
// So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.
|
|
58
|
+
|
|
59
|
+
if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
|
|
60
|
+
var _this$props$onReset2, _this$props3;
|
|
61
|
+
(_this$props$onReset2 = (_this$props3 = this.props).onReset) === null || _this$props$onReset2 === void 0 ? void 0 : _this$props$onReset2.call(_this$props3, {
|
|
62
|
+
next: resetKeys,
|
|
63
|
+
prev: prevProps.resetKeys,
|
|
64
|
+
reason: "keys"
|
|
65
|
+
});
|
|
66
|
+
this.setState(initialState);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
render() {
|
|
70
|
+
const {
|
|
71
|
+
children,
|
|
72
|
+
fallbackRender,
|
|
73
|
+
FallbackComponent,
|
|
74
|
+
fallback
|
|
75
|
+
} = this.props;
|
|
76
|
+
const {
|
|
77
|
+
didCatch,
|
|
78
|
+
error
|
|
79
|
+
} = this.state;
|
|
80
|
+
let childToRender = children;
|
|
81
|
+
if (didCatch) {
|
|
82
|
+
const props = {
|
|
83
|
+
error,
|
|
84
|
+
resetErrorBoundary: this.resetErrorBoundary
|
|
85
|
+
};
|
|
86
|
+
if (react.isValidElement(fallback)) {
|
|
87
|
+
childToRender = fallback;
|
|
88
|
+
} else if (typeof fallbackRender === "function") {
|
|
89
|
+
childToRender = fallbackRender(props);
|
|
90
|
+
} else if (FallbackComponent) {
|
|
91
|
+
childToRender = react.createElement(FallbackComponent, props);
|
|
92
|
+
} else {
|
|
93
|
+
{
|
|
94
|
+
console.error("react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop");
|
|
95
|
+
}
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return react.createElement(ErrorBoundaryContext.Provider, {
|
|
100
|
+
value: {
|
|
101
|
+
didCatch,
|
|
102
|
+
error,
|
|
103
|
+
resetErrorBoundary: this.resetErrorBoundary
|
|
104
|
+
}
|
|
105
|
+
}, childToRender);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function hasArrayChanged() {
|
|
109
|
+
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
110
|
+
let b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
111
|
+
return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function assertErrorBoundaryContext(value) {
|
|
115
|
+
if (value == null || typeof value.didCatch !== "boolean" || typeof value.resetErrorBoundary !== "function") {
|
|
116
|
+
throw new Error("ErrorBoundaryContext not found");
|
|
117
|
+
}
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function useErrorBoundary() {
|
|
122
|
+
const context = react.useContext(ErrorBoundaryContext);
|
|
123
|
+
assertErrorBoundaryContext(context);
|
|
124
|
+
const [state, setState] = react.useState({
|
|
125
|
+
error: null,
|
|
126
|
+
hasError: false
|
|
127
|
+
});
|
|
128
|
+
const memoized = react.useMemo(() => ({
|
|
129
|
+
resetBoundary: () => {
|
|
130
|
+
context === null || context === void 0 ? void 0 : context.resetErrorBoundary();
|
|
131
|
+
setState({
|
|
132
|
+
error: null,
|
|
133
|
+
hasError: false
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
showBoundary: error => setState({
|
|
137
|
+
error,
|
|
138
|
+
hasError: true
|
|
139
|
+
})
|
|
140
|
+
}), [context === null || context === void 0 ? void 0 : context.resetErrorBoundary]);
|
|
141
|
+
if (state.hasError) {
|
|
142
|
+
throw state.error;
|
|
143
|
+
}
|
|
144
|
+
return memoized;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function withErrorBoundary(component, errorBoundaryProps) {
|
|
148
|
+
const Wrapped = react.forwardRef((props, ref) => react.createElement(ErrorBoundary, errorBoundaryProps, react.createElement(component, {
|
|
149
|
+
...props,
|
|
150
|
+
ref
|
|
151
|
+
})));
|
|
152
|
+
|
|
153
|
+
// Format for display in DevTools
|
|
154
|
+
const name = component.displayName || component.name || "Unknown";
|
|
155
|
+
Wrapped.displayName = "withErrorBoundary(".concat(name, ")");
|
|
156
|
+
return Wrapped;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
exports.ErrorBoundary = ErrorBoundary;
|
|
160
|
+
exports.ErrorBoundaryContext = ErrorBoundaryContext;
|
|
161
|
+
exports.useErrorBoundary = useErrorBoundary;
|
|
162
|
+
exports.withErrorBoundary = withErrorBoundary;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { createContext, Component, isValidElement, createElement, useContext, useState, useMemo, forwardRef } from 'react';
|
|
3
|
+
|
|
4
|
+
const ErrorBoundaryContext = createContext(null);
|
|
5
|
+
|
|
6
|
+
const initialState = {
|
|
7
|
+
didCatch: false,
|
|
8
|
+
error: null
|
|
9
|
+
};
|
|
10
|
+
class ErrorBoundary extends Component {
|
|
11
|
+
constructor(props) {
|
|
12
|
+
super(props);
|
|
13
|
+
this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
|
|
14
|
+
this.state = initialState;
|
|
15
|
+
}
|
|
16
|
+
static getDerivedStateFromError(error) {
|
|
17
|
+
return {
|
|
18
|
+
didCatch: true,
|
|
19
|
+
error
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
resetErrorBoundary() {
|
|
23
|
+
const {
|
|
24
|
+
error
|
|
25
|
+
} = this.state;
|
|
26
|
+
if (error !== null) {
|
|
27
|
+
var _this$props$onReset, _this$props;
|
|
28
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
29
|
+
args[_key] = arguments[_key];
|
|
30
|
+
}
|
|
31
|
+
(_this$props$onReset = (_this$props = this.props).onReset) === null || _this$props$onReset === void 0 ? void 0 : _this$props$onReset.call(_this$props, {
|
|
32
|
+
args,
|
|
33
|
+
reason: "imperative-api"
|
|
34
|
+
});
|
|
35
|
+
this.setState(initialState);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
componentDidCatch(error, info) {
|
|
39
|
+
var _this$props$onError, _this$props2;
|
|
40
|
+
(_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 ? void 0 : _this$props$onError.call(_this$props2, error, info);
|
|
41
|
+
}
|
|
42
|
+
componentDidUpdate(prevProps, prevState) {
|
|
43
|
+
const {
|
|
44
|
+
didCatch
|
|
45
|
+
} = this.state;
|
|
46
|
+
const {
|
|
47
|
+
resetKeys
|
|
48
|
+
} = this.props;
|
|
49
|
+
|
|
50
|
+
// There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array,
|
|
51
|
+
// we'd end up resetting the error boundary immediately.
|
|
52
|
+
// This would likely trigger a second error to be thrown.
|
|
53
|
+
// So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.
|
|
54
|
+
|
|
55
|
+
if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
|
|
56
|
+
var _this$props$onReset2, _this$props3;
|
|
57
|
+
(_this$props$onReset2 = (_this$props3 = this.props).onReset) === null || _this$props$onReset2 === void 0 ? void 0 : _this$props$onReset2.call(_this$props3, {
|
|
58
|
+
next: resetKeys,
|
|
59
|
+
prev: prevProps.resetKeys,
|
|
60
|
+
reason: "keys"
|
|
61
|
+
});
|
|
62
|
+
this.setState(initialState);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
render() {
|
|
66
|
+
const {
|
|
67
|
+
children,
|
|
68
|
+
fallbackRender,
|
|
69
|
+
FallbackComponent,
|
|
70
|
+
fallback
|
|
71
|
+
} = this.props;
|
|
72
|
+
const {
|
|
73
|
+
didCatch,
|
|
74
|
+
error
|
|
75
|
+
} = this.state;
|
|
76
|
+
let childToRender = children;
|
|
77
|
+
if (didCatch) {
|
|
78
|
+
const props = {
|
|
79
|
+
error,
|
|
80
|
+
resetErrorBoundary: this.resetErrorBoundary
|
|
81
|
+
};
|
|
82
|
+
if (isValidElement(fallback)) {
|
|
83
|
+
childToRender = fallback;
|
|
84
|
+
} else if (typeof fallbackRender === "function") {
|
|
85
|
+
childToRender = fallbackRender(props);
|
|
86
|
+
} else if (FallbackComponent) {
|
|
87
|
+
childToRender = createElement(FallbackComponent, props);
|
|
88
|
+
} else {
|
|
89
|
+
{
|
|
90
|
+
console.error("react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop");
|
|
91
|
+
}
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return createElement(ErrorBoundaryContext.Provider, {
|
|
96
|
+
value: {
|
|
97
|
+
didCatch,
|
|
98
|
+
error,
|
|
99
|
+
resetErrorBoundary: this.resetErrorBoundary
|
|
100
|
+
}
|
|
101
|
+
}, childToRender);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function hasArrayChanged() {
|
|
105
|
+
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
106
|
+
let b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
107
|
+
return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function assertErrorBoundaryContext(value) {
|
|
111
|
+
if (value == null || typeof value.didCatch !== "boolean" || typeof value.resetErrorBoundary !== "function") {
|
|
112
|
+
throw new Error("ErrorBoundaryContext not found");
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function useErrorBoundary() {
|
|
118
|
+
const context = useContext(ErrorBoundaryContext);
|
|
119
|
+
assertErrorBoundaryContext(context);
|
|
120
|
+
const [state, setState] = useState({
|
|
121
|
+
error: null,
|
|
122
|
+
hasError: false
|
|
123
|
+
});
|
|
124
|
+
const memoized = useMemo(() => ({
|
|
125
|
+
resetBoundary: () => {
|
|
126
|
+
context === null || context === void 0 ? void 0 : context.resetErrorBoundary();
|
|
127
|
+
setState({
|
|
128
|
+
error: null,
|
|
129
|
+
hasError: false
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
showBoundary: error => setState({
|
|
133
|
+
error,
|
|
134
|
+
hasError: true
|
|
135
|
+
})
|
|
136
|
+
}), [context === null || context === void 0 ? void 0 : context.resetErrorBoundary]);
|
|
137
|
+
if (state.hasError) {
|
|
138
|
+
throw state.error;
|
|
139
|
+
}
|
|
140
|
+
return memoized;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function withErrorBoundary(component, errorBoundaryProps) {
|
|
144
|
+
const Wrapped = forwardRef((props, ref) => createElement(ErrorBoundary, errorBoundaryProps, createElement(component, {
|
|
145
|
+
...props,
|
|
146
|
+
ref
|
|
147
|
+
})));
|
|
148
|
+
|
|
149
|
+
// Format for display in DevTools
|
|
150
|
+
const name = component.displayName || component.name || "Unknown";
|
|
151
|
+
Wrapped.displayName = "withErrorBoundary(".concat(name, ")");
|
|
152
|
+
return Wrapped;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export { ErrorBoundary, ErrorBoundaryContext, useErrorBoundary, withErrorBoundary };
|
|
@@ -19,12 +19,16 @@ class ErrorBoundary extends Component {
|
|
|
19
19
|
error
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
|
-
resetErrorBoundary(
|
|
22
|
+
resetErrorBoundary() {
|
|
23
23
|
const {
|
|
24
24
|
error
|
|
25
25
|
} = this.state;
|
|
26
26
|
if (error !== null) {
|
|
27
|
-
|
|
27
|
+
var _this$props$onReset, _this$props;
|
|
28
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
29
|
+
args[_key] = arguments[_key];
|
|
30
|
+
}
|
|
31
|
+
(_this$props$onReset = (_this$props = this.props).onReset) === null || _this$props$onReset === void 0 ? void 0 : _this$props$onReset.call(_this$props, {
|
|
28
32
|
args,
|
|
29
33
|
reason: "imperative-api"
|
|
30
34
|
});
|
|
@@ -32,7 +36,8 @@ class ErrorBoundary extends Component {
|
|
|
32
36
|
}
|
|
33
37
|
}
|
|
34
38
|
componentDidCatch(error, info) {
|
|
35
|
-
|
|
39
|
+
var _this$props$onError, _this$props2;
|
|
40
|
+
(_this$props$onError = (_this$props2 = this.props).onError) === null || _this$props$onError === void 0 ? void 0 : _this$props$onError.call(_this$props2, error, info);
|
|
36
41
|
}
|
|
37
42
|
componentDidUpdate(prevProps, prevState) {
|
|
38
43
|
const {
|
|
@@ -48,7 +53,8 @@ class ErrorBoundary extends Component {
|
|
|
48
53
|
// So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.
|
|
49
54
|
|
|
50
55
|
if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
|
|
51
|
-
|
|
56
|
+
var _this$props$onReset2, _this$props3;
|
|
57
|
+
(_this$props$onReset2 = (_this$props3 = this.props).onReset) === null || _this$props$onReset2 === void 0 ? void 0 : _this$props$onReset2.call(_this$props3, {
|
|
52
58
|
next: resetKeys,
|
|
53
59
|
prev: prevProps.resetKeys,
|
|
54
60
|
reason: "keys"
|
|
@@ -80,7 +86,7 @@ class ErrorBoundary extends Component {
|
|
|
80
86
|
} else if (FallbackComponent) {
|
|
81
87
|
childToRender = createElement(FallbackComponent, props);
|
|
82
88
|
} else {
|
|
83
|
-
throw
|
|
89
|
+
throw error;
|
|
84
90
|
}
|
|
85
91
|
}
|
|
86
92
|
return createElement(ErrorBoundaryContext.Provider, {
|
|
@@ -92,7 +98,9 @@ class ErrorBoundary extends Component {
|
|
|
92
98
|
}, childToRender);
|
|
93
99
|
}
|
|
94
100
|
}
|
|
95
|
-
function hasArrayChanged(
|
|
101
|
+
function hasArrayChanged() {
|
|
102
|
+
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
103
|
+
let b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
96
104
|
return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
|
|
97
105
|
}
|
|
98
106
|
|
|
@@ -112,7 +120,7 @@ function useErrorBoundary() {
|
|
|
112
120
|
});
|
|
113
121
|
const memoized = useMemo(() => ({
|
|
114
122
|
resetBoundary: () => {
|
|
115
|
-
context
|
|
123
|
+
context === null || context === void 0 ? void 0 : context.resetErrorBoundary();
|
|
116
124
|
setState({
|
|
117
125
|
error: null,
|
|
118
126
|
hasError: false
|
|
@@ -122,7 +130,7 @@ function useErrorBoundary() {
|
|
|
122
130
|
error,
|
|
123
131
|
hasError: true
|
|
124
132
|
})
|
|
125
|
-
}), [context
|
|
133
|
+
}), [context === null || context === void 0 ? void 0 : context.resetErrorBoundary]);
|
|
126
134
|
if (state.hasError) {
|
|
127
135
|
throw state.error;
|
|
128
136
|
}
|
|
@@ -137,7 +145,7 @@ function withErrorBoundary(component, errorBoundaryProps) {
|
|
|
137
145
|
|
|
138
146
|
// Format for display in DevTools
|
|
139
147
|
const name = component.displayName || component.name || "Unknown";
|
|
140
|
-
Wrapped.displayName =
|
|
148
|
+
Wrapped.displayName = "withErrorBoundary(".concat(name, ")");
|
|
141
149
|
return Wrapped;
|
|
142
150
|
}
|
|
143
151
|
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-error-boundary",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.11",
|
|
4
4
|
"description": "Simple reusable React error boundary component",
|
|
5
5
|
"author": "Brian Vaughn <brian.david.vaughn@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"packageManager": "pnpm@8.6.10",
|
|
7
8
|
"repository": {
|
|
8
9
|
"type": "git",
|
|
9
10
|
"url": "https://github.com/bvaughn/react-error-boundary"
|
|
@@ -16,12 +17,23 @@
|
|
|
16
17
|
"import": "./dist/react-error-boundary.cjs.mjs",
|
|
17
18
|
"default": "./dist/react-error-boundary.cjs.js"
|
|
18
19
|
},
|
|
20
|
+
"development": {
|
|
21
|
+
"module": "./dist/react-error-boundary.development.esm.js",
|
|
22
|
+
"import": "./dist/react-error-boundary.development.cjs.mjs",
|
|
23
|
+
"default": "./dist/react-error-boundary.development.cjs.js"
|
|
24
|
+
},
|
|
19
25
|
"module": "./dist/react-error-boundary.esm.js",
|
|
20
26
|
"import": "./dist/react-error-boundary.cjs.mjs",
|
|
21
27
|
"default": "./dist/react-error-boundary.cjs.js"
|
|
22
28
|
},
|
|
23
29
|
"./package.json": "./package.json"
|
|
24
30
|
},
|
|
31
|
+
"imports": {
|
|
32
|
+
"#is-development": {
|
|
33
|
+
"development": "./src/env-conditions/development.ts",
|
|
34
|
+
"default": "./src/env-conditions/production.ts"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
25
37
|
"types": "dist/react-error-boundary.cjs.d.ts",
|
|
26
38
|
"files": [
|
|
27
39
|
"dist"
|
|
@@ -43,8 +55,9 @@
|
|
|
43
55
|
"@babel/runtime": "^7.12.5"
|
|
44
56
|
},
|
|
45
57
|
"devDependencies": {
|
|
58
|
+
"@babel/preset-env": "^7.22.5",
|
|
46
59
|
"@babel/preset-typescript": "^7.21.5",
|
|
47
|
-
"@preconstruct/cli": "^2.
|
|
60
|
+
"@preconstruct/cli": "^2.8.1",
|
|
48
61
|
"@types/jest": "^26.0.15",
|
|
49
62
|
"@types/react": "^18",
|
|
50
63
|
"@types/react-dom": "^18",
|
|
@@ -54,7 +67,7 @@
|
|
|
54
67
|
"react": "^18",
|
|
55
68
|
"react-dom": "^18",
|
|
56
69
|
"ts-jest": "^29.0.5",
|
|
57
|
-
"typescript": "^
|
|
70
|
+
"typescript": "^5.1.6"
|
|
58
71
|
},
|
|
59
72
|
"peerDependencies": {
|
|
60
73
|
"react": ">=16.13.1"
|