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
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-error-boundary",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.1",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "Simple reusable React error boundary component",
|
|
5
6
|
"author": "Brian Vaughn <brian.david.vaughn@gmail.com>",
|
|
6
7
|
"license": "MIT",
|
|
@@ -8,86 +9,121 @@
|
|
|
8
9
|
"type": "git",
|
|
9
10
|
"url": "https://github.com/bvaughn/react-error-boundary"
|
|
10
11
|
},
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
"#is-development": {
|
|
33
|
-
"development": "./src/env-conditions/development.ts",
|
|
34
|
-
"default": "./src/env-conditions/production.ts"
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
"types": "dist/react-error-boundary.cjs.d.ts",
|
|
12
|
+
"contributors": [
|
|
13
|
+
"Brian Vaughn <brian.david.vaughn@gmail.com> (https://github.com/bvaughn/)"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "http://react-error-boundary.now.sh/",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"react",
|
|
18
|
+
"reactjs",
|
|
19
|
+
"virtual",
|
|
20
|
+
"window",
|
|
21
|
+
"windowed",
|
|
22
|
+
"list",
|
|
23
|
+
"scrolling",
|
|
24
|
+
"infinite",
|
|
25
|
+
"virtualized",
|
|
26
|
+
"table",
|
|
27
|
+
"grid",
|
|
28
|
+
"spreadsheet"
|
|
29
|
+
],
|
|
30
|
+
"main": "dist/react-error-boundary.cjs",
|
|
31
|
+
"module": "dist/react-error-boundary.js",
|
|
32
|
+
"types": "dist/react-error-boundary.d.ts",
|
|
38
33
|
"files": [
|
|
39
34
|
"dist"
|
|
40
35
|
],
|
|
41
|
-
"sideEffects": false,
|
|
42
36
|
"scripts": {
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
37
|
+
"dev": "vite",
|
|
38
|
+
"build": "pnpm run build:lib && pnpm run build:docs",
|
|
39
|
+
"build:docs": "TARGET=docs vite build",
|
|
40
|
+
"build:lib": "TARGET=lib vite build",
|
|
41
|
+
"compile": "pnpm run compile:docs && pnpm run compile:examples",
|
|
42
|
+
"compile:docs": "tsx ./scripts/compile-docs",
|
|
43
|
+
"compile:examples": "tsx ./scripts/compile-examples",
|
|
47
44
|
"lint": "eslint .",
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
45
|
+
"prerelease": "rm -rf dist && pnpm run build:lib",
|
|
46
|
+
"prettier": "prettier --write \"**/*.{css,html,js,json,jsx,ts,tsx}\"",
|
|
47
|
+
"prettier:ci": "prettier --check \"**/*.{css,html,js,json,jsx,ts,tsx}\"",
|
|
48
|
+
"preview": "vite preview",
|
|
49
|
+
"test": "vitest",
|
|
50
|
+
"test:ci": "vitest run",
|
|
51
|
+
"test:debug": "vitest --inspect-brk=127.0.0.1:3000 --no-file-parallelism",
|
|
52
|
+
"tsc": "tsc -b"
|
|
53
53
|
},
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
},
|
|
57
|
-
"devDependencies": {
|
|
58
|
-
"@babel/preset-env": "^7.22.5",
|
|
59
|
-
"@babel/preset-typescript": "^7.21.5",
|
|
60
|
-
"@preconstruct/cli": "^2.8.1",
|
|
61
|
-
"@types/assert": "^1.5.10",
|
|
62
|
-
"@types/jest": "^26.0.15",
|
|
63
|
-
"@types/react": "^18.3.17",
|
|
64
|
-
"@types/react-dom": "^18",
|
|
65
|
-
"assert": "^2.0.0",
|
|
66
|
-
"eslint": "^9.13.0",
|
|
67
|
-
"eslint-config-prettier": "^9.1.0",
|
|
68
|
-
"eslint-plugin-import": "^2.25.2",
|
|
69
|
-
"eslint-plugin-prettier": "^5.2.1",
|
|
70
|
-
"eslint-plugin-react": "^7.37.2",
|
|
71
|
-
"globals": "^15.11.0",
|
|
72
|
-
"jest": "^29.4.3",
|
|
73
|
-
"jest-environment-jsdom": "^29.4.3",
|
|
74
|
-
"prettier": "^3.0.1",
|
|
75
|
-
"react": "^18",
|
|
76
|
-
"react-dom": "^18",
|
|
77
|
-
"rimraf": "^6.0.1",
|
|
78
|
-
"ts-jest": "^29.0.5",
|
|
79
|
-
"typescript": "^5.1.6",
|
|
80
|
-
"typescript-eslint": "^8.18.0"
|
|
54
|
+
"lint-staged": {
|
|
55
|
+
"**/*": "prettier --write --ignore-unknown"
|
|
81
56
|
},
|
|
82
57
|
"peerDependencies": {
|
|
83
|
-
"react": "
|
|
58
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
59
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
84
60
|
},
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
"
|
|
90
|
-
|
|
91
|
-
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@codemirror/lang-css": "latest",
|
|
63
|
+
"@codemirror/lang-html": "latest",
|
|
64
|
+
"@codemirror/lang-javascript": "latest",
|
|
65
|
+
"@codemirror/lang-markdown": "latest",
|
|
66
|
+
"@codemirror/language": "latest",
|
|
67
|
+
"@codemirror/state": "latest",
|
|
68
|
+
"@csstools/postcss-oklab-function": "^4.0.11",
|
|
69
|
+
"@eslint/js": "^9.30.1",
|
|
70
|
+
"@headlessui/react": "^2.2.4",
|
|
71
|
+
"@headlessui/tailwindcss": "^0.2.2",
|
|
72
|
+
"@heroicons/react": "^2.2.0",
|
|
73
|
+
"@lezer/highlight": "latest",
|
|
74
|
+
"@tailwindcss/vite": "^4.1.11",
|
|
75
|
+
"@tailwindplus/elements": "^1.0.5",
|
|
76
|
+
"@testing-library/jest-dom": "^6.6.4",
|
|
77
|
+
"@testing-library/react": "^16.3.0",
|
|
78
|
+
"@testing-library/user-event": "^14.6.1",
|
|
79
|
+
"@ts-ast-parser/core": "^0.8.0",
|
|
80
|
+
"@types/compression": "^1.8.1",
|
|
81
|
+
"@types/express": "^5.0.5",
|
|
82
|
+
"@types/markdown-it": "^14.1.2",
|
|
83
|
+
"@types/node": "^24.2.0",
|
|
84
|
+
"@types/react": "^19.1.8",
|
|
85
|
+
"@types/react-dom": "^19.2.3",
|
|
86
|
+
"@vitejs/plugin-react-swc": "^3.10.2",
|
|
87
|
+
"clsx": "^2.1.1",
|
|
88
|
+
"compression": "^1.8.1",
|
|
89
|
+
"csstype": "^3.1.3",
|
|
90
|
+
"eslint": "^9.30.1",
|
|
91
|
+
"eslint-plugin-react-hooks": "^5.2.0",
|
|
92
|
+
"eslint-plugin-react-refresh": "^0.4.20",
|
|
93
|
+
"express": "^5.1.0",
|
|
94
|
+
"globals": "^16.3.0",
|
|
95
|
+
"husky": "^9.1.7",
|
|
96
|
+
"jsdom": "^26.1.0",
|
|
97
|
+
"lint-staged": "^16.1.4",
|
|
98
|
+
"markdown-it": "^14.1.0",
|
|
99
|
+
"marked": "^16.4.1",
|
|
100
|
+
"postcss": "^8.5.6",
|
|
101
|
+
"prettier": "3.6.2",
|
|
102
|
+
"prettier-plugin-tailwindcss": "^0.7.1",
|
|
103
|
+
"react": "^19.2.3",
|
|
104
|
+
"react-docgen-typescript": "^2.4.0",
|
|
105
|
+
"react-dom": "^19.2.3",
|
|
106
|
+
"react-error-boundary": "^6.0.0",
|
|
107
|
+
"react-lib-tools": "^0.0.10",
|
|
108
|
+
"react-router-dom": "^7.6.3",
|
|
109
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
110
|
+
"rollup-plugin-visualizer": "^6.0.3",
|
|
111
|
+
"rollup-preserve-directives": "^1.1.3",
|
|
112
|
+
"sirv": "^3.0.2",
|
|
113
|
+
"tailwind-merge": "^3.3.1",
|
|
114
|
+
"tailwindcss": "^4.1.11",
|
|
115
|
+
"terser": "^5.43.1",
|
|
116
|
+
"ts-blank-space": "^0.6.2",
|
|
117
|
+
"ts-node": "^10.9.2",
|
|
118
|
+
"tsx": "^4.21.0",
|
|
119
|
+
"typescript": "~5.8.3",
|
|
120
|
+
"typescript-eslint": "^8.35.1",
|
|
121
|
+
"typescript-json-schema": "^0.65.1",
|
|
122
|
+
"vite": "^7.0.4",
|
|
123
|
+
"vite-plugin-dts": "^4.5.4",
|
|
124
|
+
"vite-plugin-svgr": "^4.3.0",
|
|
125
|
+
"vitest": "^3.2.4",
|
|
126
|
+
"vitest-fail-on-console": "^0.10.1",
|
|
127
|
+
"zustand": "^5.0.7"
|
|
92
128
|
}
|
|
93
129
|
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Component, ErrorInfo } from "react";
|
|
2
|
-
import { ErrorBoundaryProps } from "./types.js";
|
|
3
|
-
type ErrorBoundaryState = {
|
|
4
|
-
didCatch: true;
|
|
5
|
-
error: any;
|
|
6
|
-
} | {
|
|
7
|
-
didCatch: false;
|
|
8
|
-
error: null;
|
|
9
|
-
};
|
|
10
|
-
export declare class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
11
|
-
constructor(props: ErrorBoundaryProps);
|
|
12
|
-
static getDerivedStateFromError(error: Error): {
|
|
13
|
-
didCatch: boolean;
|
|
14
|
-
error: Error;
|
|
15
|
-
};
|
|
16
|
-
resetErrorBoundary(...args: any[]): void;
|
|
17
|
-
componentDidCatch(error: Error, info: ErrorInfo): void;
|
|
18
|
-
componentDidUpdate(prevProps: ErrorBoundaryProps, prevState: ErrorBoundaryState): void;
|
|
19
|
-
render(): import("react").FunctionComponentElement<import("react").ProviderProps<import("./ErrorBoundaryContext.js").ErrorBoundaryContextType | null>>;
|
|
20
|
-
}
|
|
21
|
-
export {};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { ComponentType, ErrorInfo, PropsWithChildren, ReactNode } from "react";
|
|
2
|
-
export type FallbackProps = {
|
|
3
|
-
error: any;
|
|
4
|
-
resetErrorBoundary: (...args: any[]) => void;
|
|
5
|
-
};
|
|
6
|
-
type ErrorBoundarySharedProps = PropsWithChildren<{
|
|
7
|
-
onError?: (error: Error, info: ErrorInfo) => void;
|
|
8
|
-
onReset?: (details: {
|
|
9
|
-
reason: "imperative-api";
|
|
10
|
-
args: any[];
|
|
11
|
-
} | {
|
|
12
|
-
reason: "keys";
|
|
13
|
-
prev: any[] | undefined;
|
|
14
|
-
next: any[] | undefined;
|
|
15
|
-
}) => void;
|
|
16
|
-
resetKeys?: any[];
|
|
17
|
-
}>;
|
|
18
|
-
export type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {
|
|
19
|
-
fallback?: never;
|
|
20
|
-
FallbackComponent: ComponentType<FallbackProps>;
|
|
21
|
-
fallbackRender?: never;
|
|
22
|
-
};
|
|
23
|
-
export type ErrorBoundaryPropsWithRender = ErrorBoundarySharedProps & {
|
|
24
|
-
fallback?: never;
|
|
25
|
-
FallbackComponent?: never;
|
|
26
|
-
fallbackRender: (props: FallbackProps) => ReactNode;
|
|
27
|
-
};
|
|
28
|
-
export type ErrorBoundaryPropsWithFallback = ErrorBoundarySharedProps & {
|
|
29
|
-
fallback: ReactNode;
|
|
30
|
-
FallbackComponent?: never;
|
|
31
|
-
fallbackRender?: never;
|
|
32
|
-
};
|
|
33
|
-
export type ErrorBoundaryProps = ErrorBoundaryPropsWithFallback | ErrorBoundaryPropsWithComponent | ErrorBoundaryPropsWithRender;
|
|
34
|
-
export {};
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { RefAttributes, ForwardRefExoticComponent, PropsWithoutRef, ComponentType, ComponentRef, ComponentProps } from "react";
|
|
2
|
-
import { ErrorBoundaryProps } from "./types.js";
|
|
3
|
-
export declare function withErrorBoundary<T extends ComponentType<any>>(component: T, errorBoundaryProps: ErrorBoundaryProps): ForwardRefExoticComponent<PropsWithoutRef<ComponentProps<T>> & RefAttributes<ComponentRef<T>>>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"react-error-boundary.cjs.d.mts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"react-error-boundary.cjs.d.ts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
|
|
@@ -1,158 +0,0 @@
|
|
|
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 (typeof fallbackRender === "function") {
|
|
87
|
-
childToRender = fallbackRender(props);
|
|
88
|
-
} else if (FallbackComponent) {
|
|
89
|
-
childToRender = react.createElement(FallbackComponent, props);
|
|
90
|
-
} else if (fallback !== undefined) {
|
|
91
|
-
childToRender = fallback;
|
|
92
|
-
} else {
|
|
93
|
-
throw error;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return react.createElement(ErrorBoundaryContext.Provider, {
|
|
97
|
-
value: {
|
|
98
|
-
didCatch,
|
|
99
|
-
error,
|
|
100
|
-
resetErrorBoundary: this.resetErrorBoundary
|
|
101
|
-
}
|
|
102
|
-
}, childToRender);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
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] : [];
|
|
108
|
-
return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function assertErrorBoundaryContext(value) {
|
|
112
|
-
if (value == null || typeof value.didCatch !== "boolean" || typeof value.resetErrorBoundary !== "function") {
|
|
113
|
-
throw new Error("ErrorBoundaryContext not found");
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function useErrorBoundary() {
|
|
118
|
-
const context = react.useContext(ErrorBoundaryContext);
|
|
119
|
-
assertErrorBoundaryContext(context);
|
|
120
|
-
const [state, setState] = react.useState({
|
|
121
|
-
error: null,
|
|
122
|
-
hasError: false
|
|
123
|
-
});
|
|
124
|
-
const memoized = react.useMemo(() => ({
|
|
125
|
-
resetBoundary: () => {
|
|
126
|
-
context.resetErrorBoundary();
|
|
127
|
-
setState({
|
|
128
|
-
error: null,
|
|
129
|
-
hasError: false
|
|
130
|
-
});
|
|
131
|
-
},
|
|
132
|
-
showBoundary: error => setState({
|
|
133
|
-
error,
|
|
134
|
-
hasError: true
|
|
135
|
-
})
|
|
136
|
-
}), [context.resetErrorBoundary]);
|
|
137
|
-
if (state.hasError) {
|
|
138
|
-
throw state.error;
|
|
139
|
-
}
|
|
140
|
-
return memoized;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function withErrorBoundary(component, errorBoundaryProps) {
|
|
144
|
-
const Wrapped = react.forwardRef((props, ref) => react.createElement(ErrorBoundary, errorBoundaryProps, react.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
|
-
exports.ErrorBoundary = ErrorBoundary;
|
|
156
|
-
exports.ErrorBoundaryContext = ErrorBoundaryContext;
|
|
157
|
-
exports.useErrorBoundary = useErrorBoundary;
|
|
158
|
-
exports.withErrorBoundary = withErrorBoundary;
|
|
@@ -1,161 +0,0 @@
|
|
|
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 (typeof fallbackRender === "function") {
|
|
87
|
-
childToRender = fallbackRender(props);
|
|
88
|
-
} else if (FallbackComponent) {
|
|
89
|
-
childToRender = react.createElement(FallbackComponent, props);
|
|
90
|
-
} else if (fallback !== undefined) {
|
|
91
|
-
childToRender = fallback;
|
|
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
|
-
}
|
|
119
|
-
|
|
120
|
-
function useErrorBoundary() {
|
|
121
|
-
const context = react.useContext(ErrorBoundaryContext);
|
|
122
|
-
assertErrorBoundaryContext(context);
|
|
123
|
-
const [state, setState] = react.useState({
|
|
124
|
-
error: null,
|
|
125
|
-
hasError: false
|
|
126
|
-
});
|
|
127
|
-
const memoized = react.useMemo(() => ({
|
|
128
|
-
resetBoundary: () => {
|
|
129
|
-
context.resetErrorBoundary();
|
|
130
|
-
setState({
|
|
131
|
-
error: null,
|
|
132
|
-
hasError: false
|
|
133
|
-
});
|
|
134
|
-
},
|
|
135
|
-
showBoundary: error => setState({
|
|
136
|
-
error,
|
|
137
|
-
hasError: true
|
|
138
|
-
})
|
|
139
|
-
}), [context.resetErrorBoundary]);
|
|
140
|
-
if (state.hasError) {
|
|
141
|
-
throw state.error;
|
|
142
|
-
}
|
|
143
|
-
return memoized;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function withErrorBoundary(component, errorBoundaryProps) {
|
|
147
|
-
const Wrapped = react.forwardRef((props, ref) => react.createElement(ErrorBoundary, errorBoundaryProps, react.createElement(component, {
|
|
148
|
-
...props,
|
|
149
|
-
ref
|
|
150
|
-
})));
|
|
151
|
-
|
|
152
|
-
// Format for display in DevTools
|
|
153
|
-
const name = component.displayName || component.name || "Unknown";
|
|
154
|
-
Wrapped.displayName = "withErrorBoundary(".concat(name, ")");
|
|
155
|
-
return Wrapped;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
exports.ErrorBoundary = ErrorBoundary;
|
|
159
|
-
exports.ErrorBoundaryContext = ErrorBoundaryContext;
|
|
160
|
-
exports.useErrorBoundary = useErrorBoundary;
|
|
161
|
-
exports.withErrorBoundary = withErrorBoundary;
|