react-error-boundary 5.0.0 → 6.0.1
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 +75 -163
- package/dist/react-error-boundary.cjs +2 -0
- package/dist/react-error-boundary.cjs.map +1 -0
- package/dist/react-error-boundary.d.ts +159 -0
- package/dist/react-error-boundary.js +111 -0
- package/dist/react-error-boundary.js.map +1 -0
- package/package.json +109 -73
- package/dist/declarations/src/ErrorBoundary.d.ts +0 -21
- package/dist/declarations/src/ErrorBoundaryContext.d.ts +0 -6
- package/dist/declarations/src/index.d.ts +0 -5
- package/dist/declarations/src/types.d.ts +0 -34
- package/dist/declarations/src/useErrorBoundary.d.ts +0 -5
- package/dist/declarations/src/withErrorBoundary.d.ts +0 -3
- package/dist/react-error-boundary.cjs.d.mts +0 -2
- package/dist/react-error-boundary.cjs.d.mts.map +0 -1
- package/dist/react-error-boundary.cjs.d.ts +0 -2
- package/dist/react-error-boundary.cjs.d.ts.map +0 -1
- package/dist/react-error-boundary.cjs.js +0 -158
- package/dist/react-error-boundary.cjs.mjs +0 -6
- package/dist/react-error-boundary.development.cjs.js +0 -161
- package/dist/react-error-boundary.development.cjs.mjs +0 -6
- package/dist/react-error-boundary.development.esm.js +0 -154
- package/dist/react-error-boundary.esm.js +0 -151
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
import { createContext, Component, 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 (typeof fallbackRender === "function") {
|
|
83
|
-
childToRender = fallbackRender(props);
|
|
84
|
-
} else if (FallbackComponent) {
|
|
85
|
-
childToRender = createElement(FallbackComponent, props);
|
|
86
|
-
} else if (fallback !== undefined) {
|
|
87
|
-
childToRender = fallback;
|
|
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
|
-
}
|
|
115
|
-
|
|
116
|
-
function useErrorBoundary() {
|
|
117
|
-
const context = useContext(ErrorBoundaryContext);
|
|
118
|
-
assertErrorBoundaryContext(context);
|
|
119
|
-
const [state, setState] = useState({
|
|
120
|
-
error: null,
|
|
121
|
-
hasError: false
|
|
122
|
-
});
|
|
123
|
-
const memoized = useMemo(() => ({
|
|
124
|
-
resetBoundary: () => {
|
|
125
|
-
context.resetErrorBoundary();
|
|
126
|
-
setState({
|
|
127
|
-
error: null,
|
|
128
|
-
hasError: false
|
|
129
|
-
});
|
|
130
|
-
},
|
|
131
|
-
showBoundary: error => setState({
|
|
132
|
-
error,
|
|
133
|
-
hasError: true
|
|
134
|
-
})
|
|
135
|
-
}), [context.resetErrorBoundary]);
|
|
136
|
-
if (state.hasError) {
|
|
137
|
-
throw state.error;
|
|
138
|
-
}
|
|
139
|
-
return memoized;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function withErrorBoundary(component, errorBoundaryProps) {
|
|
143
|
-
const Wrapped = forwardRef((props, ref) => createElement(ErrorBoundary, errorBoundaryProps, createElement(component, {
|
|
144
|
-
...props,
|
|
145
|
-
ref
|
|
146
|
-
})));
|
|
147
|
-
|
|
148
|
-
// Format for display in DevTools
|
|
149
|
-
const name = component.displayName || component.name || "Unknown";
|
|
150
|
-
Wrapped.displayName = "withErrorBoundary(".concat(name, ")");
|
|
151
|
-
return Wrapped;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export { ErrorBoundary, ErrorBoundaryContext, useErrorBoundary, withErrorBoundary };
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
import { createContext, Component, 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 (typeof fallbackRender === "function") {
|
|
83
|
-
childToRender = fallbackRender(props);
|
|
84
|
-
} else if (FallbackComponent) {
|
|
85
|
-
childToRender = createElement(FallbackComponent, props);
|
|
86
|
-
} else if (fallback !== undefined) {
|
|
87
|
-
childToRender = fallback;
|
|
88
|
-
} else {
|
|
89
|
-
throw error;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return createElement(ErrorBoundaryContext.Provider, {
|
|
93
|
-
value: {
|
|
94
|
-
didCatch,
|
|
95
|
-
error,
|
|
96
|
-
resetErrorBoundary: this.resetErrorBoundary
|
|
97
|
-
}
|
|
98
|
-
}, childToRender);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
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] : [];
|
|
104
|
-
return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function assertErrorBoundaryContext(value) {
|
|
108
|
-
if (value == null || typeof value.didCatch !== "boolean" || typeof value.resetErrorBoundary !== "function") {
|
|
109
|
-
throw new Error("ErrorBoundaryContext not found");
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function useErrorBoundary() {
|
|
114
|
-
const context = useContext(ErrorBoundaryContext);
|
|
115
|
-
assertErrorBoundaryContext(context);
|
|
116
|
-
const [state, setState] = useState({
|
|
117
|
-
error: null,
|
|
118
|
-
hasError: false
|
|
119
|
-
});
|
|
120
|
-
const memoized = useMemo(() => ({
|
|
121
|
-
resetBoundary: () => {
|
|
122
|
-
context.resetErrorBoundary();
|
|
123
|
-
setState({
|
|
124
|
-
error: null,
|
|
125
|
-
hasError: false
|
|
126
|
-
});
|
|
127
|
-
},
|
|
128
|
-
showBoundary: error => setState({
|
|
129
|
-
error,
|
|
130
|
-
hasError: true
|
|
131
|
-
})
|
|
132
|
-
}), [context.resetErrorBoundary]);
|
|
133
|
-
if (state.hasError) {
|
|
134
|
-
throw state.error;
|
|
135
|
-
}
|
|
136
|
-
return memoized;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function withErrorBoundary(component, errorBoundaryProps) {
|
|
140
|
-
const Wrapped = forwardRef((props, ref) => createElement(ErrorBoundary, errorBoundaryProps, createElement(component, {
|
|
141
|
-
...props,
|
|
142
|
-
ref
|
|
143
|
-
})));
|
|
144
|
-
|
|
145
|
-
// Format for display in DevTools
|
|
146
|
-
const name = component.displayName || component.name || "Unknown";
|
|
147
|
-
Wrapped.displayName = "withErrorBoundary(".concat(name, ")");
|
|
148
|
-
return Wrapped;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export { ErrorBoundary, ErrorBoundaryContext, useErrorBoundary, withErrorBoundary };
|