react-error-boundary 4.0.7 → 4.0.8

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.
@@ -0,0 +1,18 @@
1
+ import { Component, ErrorInfo, PropsWithChildren, PropsWithRef, ReactElement } from "react";
2
+ import { ErrorBoundaryProps } from "./types.js";
3
+ type ErrorBoundaryState = {
4
+ didCatch: boolean;
5
+ error: any;
6
+ };
7
+ export declare class ErrorBoundary extends Component<PropsWithRef<PropsWithChildren<ErrorBoundaryProps>>, ErrorBoundaryState> {
8
+ constructor(props: ErrorBoundaryProps);
9
+ static getDerivedStateFromError(error: Error): {
10
+ didCatch: boolean;
11
+ error: Error;
12
+ };
13
+ resetErrorBoundary(...args: any[]): void;
14
+ componentDidCatch(error: Error, info: ErrorInfo): void;
15
+ componentDidUpdate(prevProps: ErrorBoundaryProps, prevState: ErrorBoundaryState): void;
16
+ render(): ReactElement<any, string | import("react").JSXElementConstructor<any>>;
17
+ }
18
+ export {};
@@ -0,0 +1,6 @@
1
+ export type ErrorBoundaryContextType = {
2
+ didCatch: boolean;
3
+ error: any;
4
+ resetErrorBoundary: (...args: any[]) => void;
5
+ };
6
+ export declare const ErrorBoundaryContext: import("react").Context<ErrorBoundaryContextType | null>;
@@ -0,0 +1,5 @@
1
+ export * from "./ErrorBoundary.js";
2
+ export * from "./ErrorBoundaryContext.js";
3
+ export * from "./useErrorBoundary.js";
4
+ export * from "./withErrorBoundary.js";
5
+ export * from "./types.js";
@@ -0,0 +1,37 @@
1
+ import { Component, ComponentType, FunctionComponent, ReactElement, ReactNode } from "react";
2
+ declare function FallbackRender(props: FallbackProps): ReactNode;
3
+ export type FallbackProps = {
4
+ error: any;
5
+ resetErrorBoundary: (...args: any[]) => void;
6
+ };
7
+ type ErrorBoundarySharedProps = {
8
+ onError?: (error: Error, info: {
9
+ componentStack: string;
10
+ }) => void;
11
+ onReset?: (details: {
12
+ reason: "imperative-api";
13
+ args: any[];
14
+ } | {
15
+ reason: "keys";
16
+ prev: any[] | undefined;
17
+ next: any[] | undefined;
18
+ }) => void;
19
+ resetKeys?: any[];
20
+ };
21
+ export type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {
22
+ fallback?: never;
23
+ FallbackComponent: ComponentType<FallbackProps>;
24
+ fallbackRender?: never;
25
+ };
26
+ export type ErrorBoundaryPropsWithRender = ErrorBoundarySharedProps & {
27
+ fallback?: never;
28
+ FallbackComponent?: never;
29
+ fallbackRender: typeof FallbackRender;
30
+ };
31
+ export type ErrorBoundaryPropsWithFallback = ErrorBoundarySharedProps & {
32
+ fallback: ReactElement<unknown, string | FunctionComponent | typeof Component> | null;
33
+ FallbackComponent?: never;
34
+ fallbackRender?: never;
35
+ };
36
+ export type ErrorBoundaryProps = ErrorBoundaryPropsWithFallback | ErrorBoundaryPropsWithComponent | ErrorBoundaryPropsWithRender;
37
+ export {};
@@ -0,0 +1,5 @@
1
+ export type UseErrorBoundaryApi<Error> = {
2
+ resetBoundary: () => void;
3
+ showBoundary: (error: Error) => void;
4
+ };
5
+ export declare function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error>;
@@ -0,0 +1,3 @@
1
+ import { RefAttributes, ForwardRefExoticComponent, PropsWithoutRef, ComponentType } from "react";
2
+ import { ErrorBoundaryProps } from "./types.js";
3
+ export declare function withErrorBoundary<Props extends Object>(component: ComponentType<Props>, errorBoundaryProps: ErrorBoundaryProps): ForwardRefExoticComponent<PropsWithoutRef<Props> & RefAttributes<any>>;
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/index.js";
2
+ //# sourceMappingURL=react-error-boundary.cjs.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react-error-boundary.cjs.d.mts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/index";
2
+ //# sourceMappingURL=react-error-boundary.cjs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react-error-boundary.cjs.d.ts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
@@ -0,0 +1,151 @@
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(...args) {
27
+ const {
28
+ error
29
+ } = this.state;
30
+ if (error !== null) {
31
+ this.props.onReset?.({
32
+ args,
33
+ reason: "imperative-api"
34
+ });
35
+ this.setState(initialState);
36
+ }
37
+ }
38
+ componentDidCatch(error, info) {
39
+ this.props.onError?.(error, info);
40
+ }
41
+ componentDidUpdate(prevProps, prevState) {
42
+ const {
43
+ didCatch
44
+ } = this.state;
45
+ const {
46
+ resetKeys
47
+ } = this.props;
48
+
49
+ // There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array,
50
+ // we'd end up resetting the error boundary immediately.
51
+ // This would likely trigger a second error to be thrown.
52
+ // So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.
53
+
54
+ if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
55
+ this.props.onReset?.({
56
+ next: resetKeys,
57
+ prev: prevProps.resetKeys,
58
+ reason: "keys"
59
+ });
60
+ this.setState(initialState);
61
+ }
62
+ }
63
+ render() {
64
+ const {
65
+ children,
66
+ fallbackRender,
67
+ FallbackComponent,
68
+ fallback
69
+ } = this.props;
70
+ const {
71
+ didCatch,
72
+ error
73
+ } = this.state;
74
+ let childToRender = children;
75
+ if (didCatch) {
76
+ const props = {
77
+ error,
78
+ resetErrorBoundary: this.resetErrorBoundary
79
+ };
80
+ if (react.isValidElement(fallback)) {
81
+ childToRender = fallback;
82
+ } else if (typeof fallbackRender === "function") {
83
+ childToRender = fallbackRender(props);
84
+ } else if (FallbackComponent) {
85
+ childToRender = react.createElement(FallbackComponent, props);
86
+ } else {
87
+ throw new Error("react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop");
88
+ }
89
+ }
90
+ return react.createElement(ErrorBoundaryContext.Provider, {
91
+ value: {
92
+ didCatch,
93
+ error,
94
+ resetErrorBoundary: this.resetErrorBoundary
95
+ }
96
+ }, childToRender);
97
+ }
98
+ }
99
+ function hasArrayChanged(a = [], b = []) {
100
+ return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
101
+ }
102
+
103
+ function assertErrorBoundaryContext(value) {
104
+ if (value == null || typeof value.didCatch !== "boolean" || typeof value.resetErrorBoundary !== "function") {
105
+ throw new Error("ErrorBoundaryContext not found");
106
+ }
107
+ return true;
108
+ }
109
+
110
+ function useErrorBoundary() {
111
+ const context = react.useContext(ErrorBoundaryContext);
112
+ assertErrorBoundaryContext(context);
113
+ const [state, setState] = react.useState({
114
+ error: null,
115
+ hasError: false
116
+ });
117
+ const memoized = react.useMemo(() => ({
118
+ resetBoundary: () => {
119
+ context?.resetErrorBoundary();
120
+ setState({
121
+ error: null,
122
+ hasError: false
123
+ });
124
+ },
125
+ showBoundary: error => setState({
126
+ error,
127
+ hasError: true
128
+ })
129
+ }), [context?.resetErrorBoundary]);
130
+ if (state.hasError) {
131
+ throw state.error;
132
+ }
133
+ return memoized;
134
+ }
135
+
136
+ function withErrorBoundary(component, errorBoundaryProps) {
137
+ const Wrapped = react.forwardRef((props, ref) => react.createElement(ErrorBoundary, errorBoundaryProps, react.createElement(component, {
138
+ ...props,
139
+ ref
140
+ })));
141
+
142
+ // Format for display in DevTools
143
+ const name = component.displayName || component.name || "Unknown";
144
+ Wrapped.displayName = `withErrorBoundary(${name})`;
145
+ return Wrapped;
146
+ }
147
+
148
+ exports.ErrorBoundary = ErrorBoundary;
149
+ exports.ErrorBoundaryContext = ErrorBoundaryContext;
150
+ exports.useErrorBoundary = useErrorBoundary;
151
+ exports.withErrorBoundary = withErrorBoundary;
@@ -0,0 +1,6 @@
1
+ export {
2
+ ErrorBoundary,
3
+ ErrorBoundaryContext,
4
+ useErrorBoundary,
5
+ withErrorBoundary
6
+ } from "./react-error-boundary.cjs.js";
@@ -0,0 +1,144 @@
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(...args) {
23
+ const {
24
+ error
25
+ } = this.state;
26
+ if (error !== null) {
27
+ this.props.onReset?.({
28
+ args,
29
+ reason: "imperative-api"
30
+ });
31
+ this.setState(initialState);
32
+ }
33
+ }
34
+ componentDidCatch(error, info) {
35
+ this.props.onError?.(error, info);
36
+ }
37
+ componentDidUpdate(prevProps, prevState) {
38
+ const {
39
+ didCatch
40
+ } = this.state;
41
+ const {
42
+ resetKeys
43
+ } = this.props;
44
+
45
+ // There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array,
46
+ // we'd end up resetting the error boundary immediately.
47
+ // This would likely trigger a second error to be thrown.
48
+ // So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.
49
+
50
+ if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) {
51
+ this.props.onReset?.({
52
+ next: resetKeys,
53
+ prev: prevProps.resetKeys,
54
+ reason: "keys"
55
+ });
56
+ this.setState(initialState);
57
+ }
58
+ }
59
+ render() {
60
+ const {
61
+ children,
62
+ fallbackRender,
63
+ FallbackComponent,
64
+ fallback
65
+ } = this.props;
66
+ const {
67
+ didCatch,
68
+ error
69
+ } = this.state;
70
+ let childToRender = children;
71
+ if (didCatch) {
72
+ const props = {
73
+ error,
74
+ resetErrorBoundary: this.resetErrorBoundary
75
+ };
76
+ if (isValidElement(fallback)) {
77
+ childToRender = fallback;
78
+ } else if (typeof fallbackRender === "function") {
79
+ childToRender = fallbackRender(props);
80
+ } else if (FallbackComponent) {
81
+ childToRender = createElement(FallbackComponent, props);
82
+ } else {
83
+ throw new Error("react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop");
84
+ }
85
+ }
86
+ return createElement(ErrorBoundaryContext.Provider, {
87
+ value: {
88
+ didCatch,
89
+ error,
90
+ resetErrorBoundary: this.resetErrorBoundary
91
+ }
92
+ }, childToRender);
93
+ }
94
+ }
95
+ function hasArrayChanged(a = [], b = []) {
96
+ return a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]));
97
+ }
98
+
99
+ function assertErrorBoundaryContext(value) {
100
+ if (value == null || typeof value.didCatch !== "boolean" || typeof value.resetErrorBoundary !== "function") {
101
+ throw new Error("ErrorBoundaryContext not found");
102
+ }
103
+ return true;
104
+ }
105
+
106
+ function useErrorBoundary() {
107
+ const context = useContext(ErrorBoundaryContext);
108
+ assertErrorBoundaryContext(context);
109
+ const [state, setState] = useState({
110
+ error: null,
111
+ hasError: false
112
+ });
113
+ const memoized = useMemo(() => ({
114
+ resetBoundary: () => {
115
+ context?.resetErrorBoundary();
116
+ setState({
117
+ error: null,
118
+ hasError: false
119
+ });
120
+ },
121
+ showBoundary: error => setState({
122
+ error,
123
+ hasError: true
124
+ })
125
+ }), [context?.resetErrorBoundary]);
126
+ if (state.hasError) {
127
+ throw state.error;
128
+ }
129
+ return memoized;
130
+ }
131
+
132
+ function withErrorBoundary(component, errorBoundaryProps) {
133
+ const Wrapped = forwardRef((props, ref) => createElement(ErrorBoundary, errorBoundaryProps, createElement(component, {
134
+ ...props,
135
+ ref
136
+ })));
137
+
138
+ // Format for display in DevTools
139
+ const name = component.displayName || component.name || "Unknown";
140
+ Wrapped.displayName = `withErrorBoundary(${name})`;
141
+ return Wrapped;
142
+ }
143
+
144
+ export { ErrorBoundary, ErrorBoundaryContext, useErrorBoundary, withErrorBoundary };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-error-boundary",
3
- "version": "4.0.7",
3
+ "version": "4.0.8",
4
4
  "description": "Simple reusable React error boundary component",
5
5
  "author": "Brian Vaughn <brian.david.vaughn@gmail.com>",
6
6
  "license": "MIT",
@@ -8,20 +8,30 @@
8
8
  "type": "git",
9
9
  "url": "https://github.com/bvaughn/react-error-boundary"
10
10
  },
11
- "source": "src/index.ts",
12
- "main": "dist/react-error-boundary.js",
13
- "module": "dist/react-error-boundary.module.js",
14
- "types": "dist/react-error-boundary.d.ts",
11
+ "main": "dist/react-error-boundary.cjs.js",
12
+ "module": "dist/react-error-boundary.esm.js",
13
+ "exports": {
14
+ ".": {
15
+ "types": {
16
+ "import": "./dist/react-error-boundary.cjs.mjs",
17
+ "default": "./dist/react-error-boundary.cjs.js"
18
+ },
19
+ "module": "./dist/react-error-boundary.esm.js",
20
+ "import": "./dist/react-error-boundary.cjs.mjs",
21
+ "default": "./dist/react-error-boundary.cjs.js"
22
+ },
23
+ "./package.json": "./package.json"
24
+ },
25
+ "types": "dist/react-error-boundary.cjs.d.ts",
15
26
  "files": [
16
27
  "dist"
17
28
  ],
18
29
  "sideEffects": false,
19
30
  "scripts": {
20
- "clear": "npm run clear:parcel-cache & npm run clear:builds & npm run clear:node_modules",
31
+ "clear": "npm run clear:builds & npm run clear:node_modules",
21
32
  "clear:builds": "rm -rf ./dist",
22
- "clear:parcel-cache": "rm -rf ./.parcel-cache",
23
33
  "clear:node_modules": "rm -rf ./node_modules",
24
- "prerelease": "rm -rf ./.parcel-cache && parcel build",
34
+ "prerelease": "preconstruct build",
25
35
  "prettier": "prettier --write \"**/*.{css,html,js,json,jsx,ts,tsx}\"",
26
36
  "prettier:ci": "prettier --check \"**/*.{css,html,js,json,jsx,ts,tsx}\"",
27
37
  "test": "jest",
@@ -33,19 +43,13 @@
33
43
  "@babel/runtime": "^7.12.5"
34
44
  },
35
45
  "devDependencies": {
36
- "@parcel/config-default": "^2.9.0",
37
- "@parcel/core": "^2.9.0",
38
- "@parcel/packager-ts": "^2.9.0",
39
- "@parcel/plugin": "^2.9.0",
40
- "@parcel/source-map": "^2.1.1",
41
- "@parcel/transformer-typescript-types": "^2.9.0",
42
- "@parcel/utils": "^2.9.0",
46
+ "@babel/preset-typescript": "^7.21.5",
47
+ "@preconstruct/cli": "^2.7.0",
43
48
  "@types/jest": "^26.0.15",
44
49
  "@types/react": "^18",
45
50
  "@types/react-dom": "^18",
46
51
  "jest": "^29.4.3",
47
52
  "jest-environment-jsdom": "^29.4.3",
48
- "parcel": "^2.9.0",
49
53
  "prettier": "^2.8.6",
50
54
  "react": "^18",
51
55
  "react-dom": "^18",
@@ -62,5 +66,13 @@
62
66
  "react/no-did-update-set-state": "off",
63
67
  "babel/no-unused-expressions": "off"
64
68
  }
69
+ },
70
+ "preconstruct": {
71
+ "exports": {
72
+ "importConditionDefaultExport": "default"
73
+ },
74
+ "___experimentalFlags_WILL_CHANGE_IN_PATCH": {
75
+ "importsConditions": true
76
+ }
65
77
  }
66
78
  }
@@ -1,65 +0,0 @@
1
- import { Component, ComponentType, FunctionComponent, ReactElement, ReactNode, ErrorInfo, PropsWithChildren, PropsWithRef, RefAttributes, ForwardRefExoticComponent, PropsWithoutRef } from "react";
2
- export type ErrorBoundaryContextType = {
3
- didCatch: boolean;
4
- error: any;
5
- resetErrorBoundary: (...args: any[]) => void;
6
- };
7
- export const ErrorBoundaryContext: import("react").Context<ErrorBoundaryContextType | null>;
8
- declare function FallbackRender(props: FallbackProps): ReactNode;
9
- export type FallbackProps = {
10
- error: any;
11
- resetErrorBoundary: (...args: any[]) => void;
12
- };
13
- type ErrorBoundarySharedProps = {
14
- onError?: (error: Error, info: {
15
- componentStack: string;
16
- }) => void;
17
- onReset?: (details: {
18
- reason: "imperative-api";
19
- args: any[];
20
- } | {
21
- reason: "keys";
22
- prev: any[] | undefined;
23
- next: any[] | undefined;
24
- }) => void;
25
- resetKeys?: any[];
26
- };
27
- export type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {
28
- fallback?: never;
29
- FallbackComponent: ComponentType<FallbackProps>;
30
- fallbackRender?: never;
31
- };
32
- export type ErrorBoundaryPropsWithRender = ErrorBoundarySharedProps & {
33
- fallback?: never;
34
- FallbackComponent?: never;
35
- fallbackRender: typeof FallbackRender;
36
- };
37
- export type ErrorBoundaryPropsWithFallback = ErrorBoundarySharedProps & {
38
- fallback: ReactElement<unknown, string | FunctionComponent | typeof Component> | null;
39
- FallbackComponent?: never;
40
- fallbackRender?: never;
41
- };
42
- export type ErrorBoundaryProps = ErrorBoundaryPropsWithFallback | ErrorBoundaryPropsWithComponent | ErrorBoundaryPropsWithRender;
43
- type ErrorBoundaryState = {
44
- didCatch: boolean;
45
- error: any;
46
- };
47
- export class ErrorBoundary extends Component<PropsWithRef<PropsWithChildren<ErrorBoundaryProps>>, ErrorBoundaryState> {
48
- constructor(props: ErrorBoundaryProps);
49
- static getDerivedStateFromError(error: Error): {
50
- didCatch: boolean;
51
- error: Error;
52
- };
53
- resetErrorBoundary(...args: any[]): void;
54
- componentDidCatch(error: Error, info: ErrorInfo): void;
55
- componentDidUpdate(prevProps: ErrorBoundaryProps, prevState: ErrorBoundaryState): void;
56
- render(): ReactElement<any, string | import("react").JSXElementConstructor<any>>;
57
- }
58
- export type UseErrorBoundaryApi<Error> = {
59
- resetBoundary: () => void;
60
- showBoundary: (error: Error) => void;
61
- };
62
- export function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error>;
63
- export function withErrorBoundary<Props extends Object>(component: ComponentType<Props>, errorBoundaryProps: ErrorBounda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orBoundary.ts","src/src/withErrorBoundary.ts","src/src/index.ts","src/index.ts"],"sourcesContent":[null,null,null,null,null,null,null,"export * from \"./ErrorBoundary\";\nexport * from \"./ErrorBoundaryContext\";\nexport * from \"./useErrorBoundary\";\nexport * from \"./withErrorBoundary\";\n\n// TypeScript types\nexport * from \"./types\";\n"],"names":[],"version":3,"file":"react-error-boundary.d.ts.map"}
@@ -1,174 +0,0 @@
1
- "use client";
2
-
3
- var $8zHUo$react = require("react");
4
-
5
- function $parcel$exportWildcard(dest, source) {
6
- Object.keys(source).forEach(function(key) {
7
- if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
8
- return;
9
- }
10
-
11
- Object.defineProperty(dest, key, {
12
- enumerable: true,
13
- get: function get() {
14
- return source[key];
15
- }
16
- });
17
- });
18
-
19
- return dest;
20
- }
21
- function $parcel$export(e, n, v, s) {
22
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
23
- }
24
- var $6d6d4999e62b3ee0$exports = {};
25
-
26
- $parcel$export($6d6d4999e62b3ee0$exports, "ErrorBoundary", () => $6d6d4999e62b3ee0$export$e926676385687eaf);
27
-
28
- var $4a61716688322eb0$exports = {};
29
-
30
- $parcel$export($4a61716688322eb0$exports, "ErrorBoundaryContext", () => $4a61716688322eb0$export$b16d9fb1a22de840);
31
-
32
- const $4a61716688322eb0$export$b16d9fb1a22de840 = (0, $8zHUo$react.createContext)(null);
33
-
34
-
35
- const $6d6d4999e62b3ee0$var$initialState = {
36
- didCatch: false,
37
- error: null
38
- };
39
- class $6d6d4999e62b3ee0$export$e926676385687eaf extends (0, $8zHUo$react.Component) {
40
- constructor(props){
41
- super(props);
42
- this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
43
- this.state = $6d6d4999e62b3ee0$var$initialState;
44
- }
45
- static getDerivedStateFromError(error) {
46
- return {
47
- didCatch: true,
48
- error: error
49
- };
50
- }
51
- resetErrorBoundary(...args) {
52
- const { error: error } = this.state;
53
- if (error !== null) {
54
- this.props.onReset?.({
55
- args: args,
56
- reason: "imperative-api"
57
- });
58
- this.setState($6d6d4999e62b3ee0$var$initialState);
59
- }
60
- }
61
- componentDidCatch(error, info) {
62
- this.props.onError?.(error, info);
63
- }
64
- componentDidUpdate(prevProps, prevState) {
65
- const { didCatch: didCatch } = this.state;
66
- const { resetKeys: resetKeys } = this.props;
67
- // There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array,
68
- // we'd end up resetting the error boundary immediately.
69
- // This would likely trigger a second error to be thrown.
70
- // So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.
71
- if (didCatch && prevState.error !== null && $6d6d4999e62b3ee0$var$hasArrayChanged(prevProps.resetKeys, resetKeys)) {
72
- this.props.onReset?.({
73
- next: resetKeys,
74
- prev: prevProps.resetKeys,
75
- reason: "keys"
76
- });
77
- this.setState($6d6d4999e62b3ee0$var$initialState);
78
- }
79
- }
80
- render() {
81
- const { children: children , fallbackRender: fallbackRender , FallbackComponent: FallbackComponent , fallback: fallback } = this.props;
82
- const { didCatch: didCatch , error: error } = this.state;
83
- let childToRender = children;
84
- if (didCatch) {
85
- const props = {
86
- error: error,
87
- resetErrorBoundary: this.resetErrorBoundary
88
- };
89
- if ((0, $8zHUo$react.isValidElement)(fallback)) childToRender = fallback;
90
- else if (typeof fallbackRender === "function") childToRender = fallbackRender(props);
91
- else if (FallbackComponent) childToRender = (0, $8zHUo$react.createElement)(FallbackComponent, props);
92
- else throw new Error("react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop");
93
- }
94
- return (0, $8zHUo$react.createElement)((0, $4a61716688322eb0$export$b16d9fb1a22de840).Provider, {
95
- value: {
96
- didCatch: didCatch,
97
- error: error,
98
- resetErrorBoundary: this.resetErrorBoundary
99
- }
100
- }, childToRender);
101
- }
102
- }
103
- function $6d6d4999e62b3ee0$var$hasArrayChanged(a = [], b = []) {
104
- return a.length !== b.length || a.some((item, index)=>!Object.is(item, b[index]));
105
- }
106
-
107
-
108
-
109
- var $3c4937b727a6fcfb$exports = {};
110
-
111
- $parcel$export($3c4937b727a6fcfb$exports, "useErrorBoundary", () => $3c4937b727a6fcfb$export$c052f6604b7d51fe);
112
-
113
- function $f8678e3a8e88e8a4$export$f20aa86254872370(value) {
114
- if (value == null || typeof value.didCatch !== "boolean" || typeof value.resetErrorBoundary !== "function") throw new Error("ErrorBoundaryContext not found");
115
- return true;
116
- }
117
-
118
-
119
-
120
- function $3c4937b727a6fcfb$export$c052f6604b7d51fe() {
121
- const context = (0, $8zHUo$react.useContext)((0, $4a61716688322eb0$export$b16d9fb1a22de840));
122
- (0, $f8678e3a8e88e8a4$export$f20aa86254872370)(context);
123
- const [state, setState] = (0, $8zHUo$react.useState)({
124
- error: null,
125
- hasError: false
126
- });
127
- const memoized = (0, $8zHUo$react.useMemo)(()=>({
128
- resetBoundary: ()=>{
129
- context?.resetErrorBoundary();
130
- setState({
131
- error: null,
132
- hasError: false
133
- });
134
- },
135
- showBoundary: (error)=>setState({
136
- error: error,
137
- hasError: true
138
- })
139
- }), [
140
- context?.resetErrorBoundary
141
- ]);
142
- if (state.hasError) throw state.error;
143
- return memoized;
144
- }
145
-
146
-
147
- var $9e88dd86e0bb2944$exports = {};
148
-
149
- $parcel$export($9e88dd86e0bb2944$exports, "withErrorBoundary", () => $9e88dd86e0bb2944$export$f0c7a449e0cfaec7);
150
-
151
-
152
- function $9e88dd86e0bb2944$export$f0c7a449e0cfaec7(component, errorBoundaryProps) {
153
- const Wrapped = (0, $8zHUo$react.forwardRef)((props, ref)=>(0, $8zHUo$react.createElement)((0, $6d6d4999e62b3ee0$export$e926676385687eaf), errorBoundaryProps, (0, $8zHUo$react.createElement)(component, {
154
- ...props,
155
- ref: ref
156
- })));
157
- // Format for display in DevTools
158
- const name = component.displayName || component.name || "Unknown";
159
- Wrapped.displayName = `withErrorBoundary(${name})`;
160
- return Wrapped;
161
- }
162
-
163
-
164
- var $faefaad95e5fcca0$exports = {};
165
-
166
-
167
- $parcel$exportWildcard(module.exports, $6d6d4999e62b3ee0$exports);
168
- $parcel$exportWildcard(module.exports, $4a61716688322eb0$exports);
169
- $parcel$exportWildcard(module.exports, $3c4937b727a6fcfb$exports);
170
- $parcel$exportWildcard(module.exports, $9e88dd86e0bb2944$exports);
171
- $parcel$exportWildcard(module.exports, $faefaad95e5fcca0$exports);
172
-
173
-
174
- //# sourceMappingURL=react-error-boundary.js.map
@@ -1 +0,0 @@
1
- {"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AEQO,MAAM,4CACX,CAAA,GAAA,0BAAY,EAAmC;;;ADKjD,MAAM,qCAAmC;IACvC,UAAU;IACV,OAAO;AACT;AAEO,MAAM,kDAAsB,CAAA,GAAA,sBAAQ;IAIzC,YAAY,KAAyB,CAAE;QACrC,KAAK,CAAC;QAEN,IAAI,CAAC,qBAAqB,IAAI,CAAC,mBAAmB,KAAK,IAAI;QAC3D,IAAI,CAAC,QAAQ;IACf;IAEA,OAAO,yBAAyB,KAAY,EAAE;QAC5C,OAAO;YAAE,UAAU;mBAAM;QAAM;IACjC;IAEA,mBAAmB,GAAG,IAAW,EAAE;QACjC,MAAM,SAAE,MAAK,EAAE,GAAG,IAAI,CAAC;QAEvB,IAAI,UAAU,MAAM;YAClB,IAAI,CAAC,MAAM,UAAU;sBACnB;gBACA,QAAQ;YACV;YAEA,IAAI,CAAC,SAAS;QAChB;IACF;IAEA,kBAAkB,KAAY,EAAE,IAAe,EAAE;QAC/C,IAAI,CAAC,MAAM,UAAU,OAAO;IAC9B;IAEA,mBACE,SAA6B,EAC7B,SAA6B,EAC7B;QACA,MAAM,YAAE,SAAQ,EAAE,GAAG,IAAI,CAAC;QAC1B,MAAM,aAAE,UAAS,EAAE,GAAG,IAAI,CAAC;QAE3B,gHAAgH;QAChH,wDAAwD;QACxD,yDAAyD;QACzD,qGAAqG;QAErG,IACE,YACA,UAAU,UAAU,QACpB,sCAAgB,UAAU,WAAW,YACrC;YACA,IAAI,CAAC,MAAM,UAAU;gBACnB,MA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EACrB,IAAO,CAAA;YACL,eAAe;gBACb,SAAS;gBACT,SAAS;oBAAE,OAAO;oBAAM,UAAU;gBAAM;YAC1C;YACA,cAAc,CAAC,QACb,SAAS;2BACP;oBACA,UAAU;gBACZ;QACJ,CAAA,GACA;QAAC,SAAS;KAAmB;IAG/B,IAAI,MAAM,UACR,MAAM,MAAM;IAGd,OAAO;AACT;;;;;;;;AE9BO,SAAS,0CACd,SAA+B,EAC/B,kBAAsC;IAEtC,MAAM,UAAU,CAAA,GAAA,uBAAS,EACvB,CAAC,OAAc,MACb,CAAA,GAAA,0BAAY,EACV,CAAA,GAAA,yCAAY,GACZ,oBACA,CAAA,GAAA,0BAAY,EAAE,WAAW;YAAE,GAAG,KAAK;iBAAE;QAAI;IAI/C,iCAAiC;IACjC,MAAM,OAAO,UAAU,eAAe,UAAU,QAAQ;IACxD,QAAQ,cAAc,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;IAElD,OAAO;AACT;;;;;","sources":["src/index.ts","src/ErrorBoundary.ts","src/ErrorBoundaryContext.ts","src/useErrorBoundary.ts","src/assertErrorBoundaryContext.ts","src/withErrorBoundary.ts","src/types.ts"],"sourcesContent":["export * from \"./ErrorBoundary\";\nexport * from \"./ErrorBoundaryContext\";\nexport * from \"./useErrorBoundary\";\nexport * from \"./withErrorBoundary\";\n\n// TypeScript types\nexport * from \"./types\";\n","import {\n Component,\n createElement,\n ErrorInfo,\n isValidElement,\n PropsWithChildren,\n PropsWithRef,\n ReactElement,\n} from \"react\";\nimport { ErrorBoundaryContext } from \"./ErrorBoundaryContext\";\nimport { ErrorBoundaryProps, FallbackProps } from \"./types\";\n\ntype ErrorBoundaryState = { didCatch: boolean; error: any };\n\nconst initialState: ErrorBoundaryState = {\n didCatch: false,\n error: null,\n};\n\nexport class ErrorBoundary extends Component<\n PropsWithRef<PropsWithChildren<ErrorBoundaryProps>>,\n ErrorBoundaryState\n> {\n constructor(props: ErrorBoundaryProps) {\n super(props);\n\n this.resetErrorBoundary = this.resetErrorBoundary.bind(this);\n this.state = initialState;\n }\n\n static getDerivedStateFromError(error: Error) {\n return { didCatch: true, error };\n }\n\n resetErrorBoundary(...args: any[]) {\n const { error } = this.state;\n\n if (error !== null) {\n this.props.onReset?.({\n args,\n reason: \"imperative-api\",\n });\n\n this.setState(initialState);\n }\n }\n\n componentDidCatch(error: Error, info: ErrorInfo) {\n this.props.onError?.(error, info);\n }\n\n componentDidUpdate(\n prevProps: ErrorBoundaryProps,\n prevState: ErrorBoundaryState\n ) {\n const { didCatch } = this.state;\n const { resetKeys } = this.props;\n\n // There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array,\n // we'd end up resetting the error boundary immediately.\n // This would likely trigger a second error to be thrown.\n // So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.\n\n if (\n didCatch &&\n prevState.error !== null &&\n hasArrayChanged(prevProps.resetKeys, resetKeys)\n ) {\n this.props.onReset?.({\n next: resetKeys,\n prev: prevProps.resetKeys,\n reason: \"keys\",\n });\n\n this.setState(initialState);\n }\n }\n\n render() {\n const { children, fallbackRender, FallbackComponent, fallback } =\n this.props;\n const { didCatch, error } = this.state;\n\n let childToRender = children;\n\n if (didCatch) {\n const props: FallbackProps = {\n error,\n resetErrorBoundary: this.resetErrorBoundary,\n };\n\n if (isValidElement(fallback)) {\n childToRender = fallback;\n } else if (typeof fallbackRender === \"function\") {\n childToRender = fallbackRender(props);\n } else if (FallbackComponent) {\n childToRender = createElement(FallbackComponent, props);\n } else {\n throw new Error(\n \"react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop\"\n );\n }\n }\n\n return createElement(\n ErrorBoundaryContext.Provider,\n {\n value: {\n didCatch,\n error,\n resetErrorBoundary: this.resetErrorBoundary,\n },\n },\n childToRender\n ) as ReactElement;\n }\n}\n\nfunction hasArrayChanged(a: any[] = [], b: any[] = []) {\n return (\n a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]))\n );\n}\n","import { createContext } from \"react\";\n\nexport type ErrorBoundaryContextType = {\n didCatch: boolean;\n error: any;\n resetErrorBoundary: (...args: any[]) => void;\n};\n\nexport const ErrorBoundaryContext =\n createContext<ErrorBoundaryContextType | null>(null);\n","import { useContext, useMemo, useState } from \"react\";\nimport { assertErrorBoundaryContext } from \"./assertErrorBoundaryContext\";\nimport { ErrorBoundaryContext } from \"./ErrorBoundaryContext\";\n\nexport type UseErrorBoundaryApi<Error> = {\n resetBoundary: () => void;\n showBoundary: (error: Error) => void;\n};\n\nexport function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error> {\n const context = useContext(ErrorBoundaryContext);\n\n assertErrorBoundaryContext(context);\n\n const [state, setState] = useState<{\n error: Error | null;\n hasError: boolean;\n }>({\n error: null,\n hasError: false,\n });\n\n const memoized = useMemo(\n () => ({\n resetBoundary: () => {\n context?.resetErrorBoundary();\n setState({ error: null, hasError: false });\n },\n showBoundary: (error: Error) =>\n setState({\n error,\n hasError: true,\n }),\n }),\n [context?.resetErrorBoundary]\n );\n\n if (state.hasError) {\n throw state.error;\n }\n\n return memoized;\n}\n","import { ErrorBoundaryContextType } from \"./ErrorBoundaryContext\";\n\nexport function assertErrorBoundaryContext(\n value: any\n): value is ErrorBoundaryContextType {\n if (\n value == null ||\n typeof value.didCatch !== \"boolean\" ||\n typeof value.resetErrorBoundary !== \"function\"\n ) {\n throw new Error(\"ErrorBoundaryContext not found\");\n }\n\n return true;\n}\n","import {\n createElement,\n forwardRef,\n ForwardedRef,\n RefAttributes,\n ForwardRefExoticComponent,\n PropsWithoutRef,\n ComponentType,\n} from \"react\";\nimport { ErrorBoundary } from \"./ErrorBoundary\";\nimport { ErrorBoundaryProps } from \"./types\";\n\nexport function withErrorBoundary<Props extends Object>(\n component: ComponentType<Props>,\n errorBoundaryProps: ErrorBoundaryProps\n): ForwardRefExoticComponent<PropsWithoutRef<Props> & RefAttributes<any>> {\n const Wrapped = forwardRef<ComponentType<Props>, Props>(\n (props: Props, ref: ForwardedRef<ComponentType<Props>>) =>\n createElement(\n ErrorBoundary,\n errorBoundaryProps,\n createElement(component, { ...props, ref })\n )\n );\n\n // Format for display in DevTools\n const name = component.displayName || component.name || \"Unknown\";\n Wrapped.displayName = `withErrorBoundary(${name})`;\n\n return Wrapped;\n}\n","import {\n Component,\n ComponentType,\n FunctionComponent,\n ReactElement,\n ReactNode,\n} from \"react\";\n\ndeclare function FallbackRender(props: FallbackProps): ReactNode;\n\nexport type FallbackProps = {\n error: any;\n resetErrorBoundary: (...args: any[]) => void;\n};\n\ntype ErrorBoundarySharedProps = {\n onError?: (error: Error, info: { componentStack: string }) => void;\n onReset?: (\n details:\n | { reason: \"imperative-api\"; args: any[] }\n | { reason: \"keys\"; prev: any[] | undefined; next: any[] | undefined }\n ) => void;\n resetKeys?: any[];\n};\n\nexport type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {\n fallback?: never;\n FallbackComponent: ComponentType<FallbackProps>;\n fallbackRender?: never;\n};\n\nexport type ErrorBoundaryPropsWithRender = ErrorBoundarySharedProps & {\n fallback?: never;\n FallbackComponent?: never;\n fallbackRender: typeof FallbackRender;\n};\n\nexport type ErrorBoundaryPropsWithFallback = ErrorBoundarySharedProps & {\n fallback: ReactElement<\n unknown,\n string | FunctionComponent | typeof Component\n > | null;\n FallbackComponent?: never;\n fallbackRender?: never;\n};\n\nexport type ErrorBoundaryProps =\n | ErrorBoundaryPropsWithFallback\n | ErrorBoundaryPropsWithComponent\n | ErrorBoundaryPropsWithRender;\n"],"names":[],"version":3,"file":"react-error-boundary.js.map"}
@@ -1,154 +0,0 @@
1
- "use client";
2
-
3
- import {isValidElement as $hgUW1$isValidElement, createElement as $hgUW1$createElement, Component as $hgUW1$Component, createContext as $hgUW1$createContext, useContext as $hgUW1$useContext, useState as $hgUW1$useState, useMemo as $hgUW1$useMemo, forwardRef as $hgUW1$forwardRef} from "react";
4
-
5
- function $parcel$export(e, n, v, s) {
6
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
7
- }
8
- var $44d7e150ebc754d2$exports = {};
9
-
10
- $parcel$export($44d7e150ebc754d2$exports, "ErrorBoundary", () => $44d7e150ebc754d2$export$e926676385687eaf);
11
-
12
- var $ebb31c7feaa4405e$exports = {};
13
-
14
- $parcel$export($ebb31c7feaa4405e$exports, "ErrorBoundaryContext", () => $ebb31c7feaa4405e$export$b16d9fb1a22de840);
15
-
16
- const $ebb31c7feaa4405e$export$b16d9fb1a22de840 = (0, $hgUW1$createContext)(null);
17
-
18
-
19
- const $44d7e150ebc754d2$var$initialState = {
20
- didCatch: false,
21
- error: null
22
- };
23
- class $44d7e150ebc754d2$export$e926676385687eaf extends (0, $hgUW1$Component) {
24
- constructor(props){
25
- super(props);
26
- this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
27
- this.state = $44d7e150ebc754d2$var$initialState;
28
- }
29
- static getDerivedStateFromError(error) {
30
- return {
31
- didCatch: true,
32
- error: error
33
- };
34
- }
35
- resetErrorBoundary(...args) {
36
- const { error: error } = this.state;
37
- if (error !== null) {
38
- this.props.onReset?.({
39
- args: args,
40
- reason: "imperative-api"
41
- });
42
- this.setState($44d7e150ebc754d2$var$initialState);
43
- }
44
- }
45
- componentDidCatch(error, info) {
46
- this.props.onError?.(error, info);
47
- }
48
- componentDidUpdate(prevProps, prevState) {
49
- const { didCatch: didCatch } = this.state;
50
- const { resetKeys: resetKeys } = this.props;
51
- // There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array,
52
- // we'd end up resetting the error boundary immediately.
53
- // This would likely trigger a second error to be thrown.
54
- // So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.
55
- if (didCatch && prevState.error !== null && $44d7e150ebc754d2$var$hasArrayChanged(prevProps.resetKeys, resetKeys)) {
56
- this.props.onReset?.({
57
- next: resetKeys,
58
- prev: prevProps.resetKeys,
59
- reason: "keys"
60
- });
61
- this.setState($44d7e150ebc754d2$var$initialState);
62
- }
63
- }
64
- render() {
65
- const { children: children , fallbackRender: fallbackRender , FallbackComponent: FallbackComponent , fallback: fallback } = this.props;
66
- const { didCatch: didCatch , error: error } = this.state;
67
- let childToRender = children;
68
- if (didCatch) {
69
- const props = {
70
- error: error,
71
- resetErrorBoundary: this.resetErrorBoundary
72
- };
73
- if ((0, $hgUW1$isValidElement)(fallback)) childToRender = fallback;
74
- else if (typeof fallbackRender === "function") childToRender = fallbackRender(props);
75
- else if (FallbackComponent) childToRender = (0, $hgUW1$createElement)(FallbackComponent, props);
76
- else throw new Error("react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop");
77
- }
78
- return (0, $hgUW1$createElement)((0, $ebb31c7feaa4405e$export$b16d9fb1a22de840).Provider, {
79
- value: {
80
- didCatch: didCatch,
81
- error: error,
82
- resetErrorBoundary: this.resetErrorBoundary
83
- }
84
- }, childToRender);
85
- }
86
- }
87
- function $44d7e150ebc754d2$var$hasArrayChanged(a = [], b = []) {
88
- return a.length !== b.length || a.some((item, index)=>!Object.is(item, b[index]));
89
- }
90
-
91
-
92
-
93
- var $7c3c25b3f398a9d6$exports = {};
94
-
95
- $parcel$export($7c3c25b3f398a9d6$exports, "useErrorBoundary", () => $7c3c25b3f398a9d6$export$c052f6604b7d51fe);
96
-
97
- function $75c9d331f9c1ed1a$export$f20aa86254872370(value) {
98
- if (value == null || typeof value.didCatch !== "boolean" || typeof value.resetErrorBoundary !== "function") throw new Error("ErrorBoundaryContext not found");
99
- return true;
100
- }
101
-
102
-
103
-
104
- function $7c3c25b3f398a9d6$export$c052f6604b7d51fe() {
105
- const context = (0, $hgUW1$useContext)((0, $ebb31c7feaa4405e$export$b16d9fb1a22de840));
106
- (0, $75c9d331f9c1ed1a$export$f20aa86254872370)(context);
107
- const [state, setState] = (0, $hgUW1$useState)({
108
- error: null,
109
- hasError: false
110
- });
111
- const memoized = (0, $hgUW1$useMemo)(()=>({
112
- resetBoundary: ()=>{
113
- context?.resetErrorBoundary();
114
- setState({
115
- error: null,
116
- hasError: false
117
- });
118
- },
119
- showBoundary: (error)=>setState({
120
- error: error,
121
- hasError: true
122
- })
123
- }), [
124
- context?.resetErrorBoundary
125
- ]);
126
- if (state.hasError) throw state.error;
127
- return memoized;
128
- }
129
-
130
-
131
- var $62ff477d53f02a5b$exports = {};
132
-
133
- $parcel$export($62ff477d53f02a5b$exports, "withErrorBoundary", () => $62ff477d53f02a5b$export$f0c7a449e0cfaec7);
134
-
135
-
136
- function $62ff477d53f02a5b$export$f0c7a449e0cfaec7(component, errorBoundaryProps) {
137
- const Wrapped = (0, $hgUW1$forwardRef)((props, ref)=>(0, $hgUW1$createElement)((0, $44d7e150ebc754d2$export$e926676385687eaf), errorBoundaryProps, (0, $hgUW1$createElement)(component, {
138
- ...props,
139
- ref: ref
140
- })));
141
- // Format for display in DevTools
142
- const name = component.displayName || component.name || "Unknown";
143
- Wrapped.displayName = `withErrorBoundary(${name})`;
144
- return Wrapped;
145
- }
146
-
147
-
148
- var $81c1b644006d48ec$exports = {};
149
-
150
-
151
-
152
-
153
- export {$44d7e150ebc754d2$export$e926676385687eaf as ErrorBoundary, $ebb31c7feaa4405e$export$b16d9fb1a22de840 as ErrorBoundaryContext, $7c3c25b3f398a9d6$export$c052f6604b7d51fe as useErrorBoundary, $62ff477d53f02a5b$export$f0c7a449e0cfaec7 as withErrorBoundary};
154
- //# sourceMappingURL=react-error-boundary.module.js.map
@@ -1 +0,0 @@
1
- {"mappings":";;;;;;;;;;;;;;;AEQO,MAAM,4CACX,CAAA,GAAA,oBAAY,EAAmC;;;ADKjD,MAAM,qCAAmC;IACvC,UAAU;IACV,OAAO;AACT;AAEO,MAAM,kDAAsB,CAAA,GAAA,gBAAQ;IAIzC,YAAY,KAAyB,CAAE;QACrC,KAAK,CAAC;QAEN,IAAI,CAAC,qBAAqB,IAAI,CAAC,mBAAmB,KAAK,IAAI;QAC3D,IAAI,CAAC,QAAQ;IACf;IAEA,OAAO,yBAAyB,KAAY,EAAE;QAC5C,OAAO;YAAE,UAAU;mBAAM;QAAM;IACjC;IAEA,mBAAmB,GAAG,IAAW,EA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ext.ts","src/withErrorBoundary.ts","src/types.ts"],"sourcesContent":["export * from \"./ErrorBoundary\";\nexport * from \"./ErrorBoundaryContext\";\nexport * from \"./useErrorBoundary\";\nexport * from \"./withErrorBoundary\";\n\n// TypeScript types\nexport * from \"./types\";\n","import {\n Component,\n createElement,\n ErrorInfo,\n isValidElement,\n PropsWithChildren,\n PropsWithRef,\n ReactElement,\n} from \"react\";\nimport { ErrorBoundaryContext } from \"./ErrorBoundaryContext\";\nimport { ErrorBoundaryProps, FallbackProps } from \"./types\";\n\ntype ErrorBoundaryState = { didCatch: boolean; error: any };\n\nconst initialState: ErrorBoundaryState = {\n didCatch: false,\n error: null,\n};\n\nexport class ErrorBoundary extends Component<\n PropsWithRef<PropsWithChildren<ErrorBoundaryProps>>,\n ErrorBoundaryState\n> {\n constructor(props: ErrorBoundaryProps) {\n super(props);\n\n this.resetErrorBoundary = this.resetErrorBoundary.bind(this);\n this.state = initialState;\n }\n\n static getDerivedStateFromError(error: Error) {\n return { didCatch: true, error };\n }\n\n resetErrorBoundary(...args: any[]) {\n const { error } = this.state;\n\n if (error !== null) {\n this.props.onReset?.({\n args,\n reason: \"imperative-api\",\n });\n\n this.setState(initialState);\n }\n }\n\n componentDidCatch(error: Error, info: ErrorInfo) {\n this.props.onError?.(error, info);\n }\n\n componentDidUpdate(\n prevProps: ErrorBoundaryProps,\n prevState: ErrorBoundaryState\n ) {\n const { didCatch } = this.state;\n const { resetKeys } = this.props;\n\n // There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array,\n // we'd end up resetting the error boundary immediately.\n // This would likely trigger a second error to be thrown.\n // So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.\n\n if (\n didCatch &&\n prevState.error !== null &&\n hasArrayChanged(prevProps.resetKeys, resetKeys)\n ) {\n this.props.onReset?.({\n next: resetKeys,\n prev: prevProps.resetKeys,\n reason: \"keys\",\n });\n\n this.setState(initialState);\n }\n }\n\n render() {\n const { children, fallbackRender, FallbackComponent, fallback } =\n this.props;\n const { didCatch, error } = this.state;\n\n let childToRender = children;\n\n if (didCatch) {\n const props: FallbackProps = {\n error,\n resetErrorBoundary: this.resetErrorBoundary,\n };\n\n if (isValidElement(fallback)) {\n childToRender = fallback;\n } else if (typeof fallbackRender === \"function\") {\n childToRender = fallbackRender(props);\n } else if (FallbackComponent) {\n childToRender = createElement(FallbackComponent, props);\n } else {\n throw new Error(\n \"react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop\"\n );\n }\n }\n\n return createElement(\n ErrorBoundaryContext.Provider,\n {\n value: {\n didCatch,\n error,\n resetErrorBoundary: this.resetErrorBoundary,\n },\n },\n childToRender\n ) as ReactElement;\n }\n}\n\nfunction hasArrayChanged(a: any[] = [], b: any[] = []) {\n return (\n a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]))\n );\n}\n","import { createContext } from \"react\";\n\nexport type ErrorBoundaryContextType = {\n didCatch: boolean;\n error: any;\n resetErrorBoundary: (...args: any[]) => void;\n};\n\nexport const ErrorBoundaryContext =\n createContext<ErrorBoundaryContextType | null>(null);\n","import { useContext, useMemo, useState } from \"react\";\nimport { assertErrorBoundaryContext } from \"./assertErrorBoundaryContext\";\nimport { ErrorBoundaryContext } from \"./ErrorBoundaryContext\";\n\nexport type UseErrorBoundaryApi<Error> = {\n resetBoundary: () => void;\n showBoundary: (error: Error) => void;\n};\n\nexport function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error> {\n const context = useContext(ErrorBoundaryContext);\n\n assertErrorBoundaryContext(context);\n\n const [state, setState] = useState<{\n error: Error | null;\n hasError: boolean;\n }>({\n error: null,\n hasError: false,\n });\n\n const memoized = useMemo(\n () => ({\n resetBoundary: () => {\n context?.resetErrorBoundary();\n setState({ error: null, hasError: false });\n },\n showBoundary: (error: Error) =>\n setState({\n error,\n hasError: true,\n }),\n }),\n [context?.resetErrorBoundary]\n );\n\n if (state.hasError) {\n throw state.error;\n }\n\n return memoized;\n}\n","import { ErrorBoundaryContextType } from \"./ErrorBoundaryContext\";\n\nexport function assertErrorBoundaryContext(\n value: any\n): value is ErrorBoundaryContextType {\n if (\n value == null ||\n typeof value.didCatch !== \"boolean\" ||\n typeof value.resetErrorBoundary !== \"function\"\n ) {\n throw new Error(\"ErrorBoundaryContext not found\");\n }\n\n return true;\n}\n","import {\n createElement,\n forwardRef,\n ForwardedRef,\n RefAttributes,\n ForwardRefExoticComponent,\n PropsWithoutRef,\n ComponentType,\n} from \"react\";\nimport { ErrorBoundary } from \"./ErrorBoundary\";\nimport { ErrorBoundaryProps } from \"./types\";\n\nexport function withErrorBoundary<Props extends Object>(\n component: ComponentType<Props>,\n errorBoundaryProps: ErrorBoundaryProps\n): ForwardRefExoticComponent<PropsWithoutRef<Props> & RefAttributes<any>> {\n const Wrapped = forwardRef<ComponentType<Props>, Props>(\n (props: Props, ref: ForwardedRef<ComponentType<Props>>) =>\n createElement(\n ErrorBoundary,\n errorBoundaryProps,\n createElement(component, { ...props, ref })\n )\n );\n\n // Format for display in DevTools\n const name = component.displayName || component.name || \"Unknown\";\n Wrapped.displayName = `withErrorBoundary(${name})`;\n\n return Wrapped;\n}\n","import {\n Component,\n ComponentType,\n FunctionComponent,\n ReactElement,\n ReactNode,\n} from \"react\";\n\ndeclare function FallbackRender(props: FallbackProps): ReactNode;\n\nexport type FallbackProps = {\n error: any;\n resetErrorBoundary: (...args: any[]) => void;\n};\n\ntype ErrorBoundarySharedProps = {\n onError?: (error: Error, info: { componentStack: string }) => void;\n onReset?: (\n details:\n | { reason: \"imperative-api\"; args: any[] }\n | { reason: \"keys\"; prev: any[] | undefined; next: any[] | undefined }\n ) => void;\n resetKeys?: any[];\n};\n\nexport type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {\n fallback?: never;\n FallbackComponent: ComponentType<FallbackProps>;\n fallbackRender?: never;\n};\n\nexport type ErrorBoundaryPropsWithRender = ErrorBoundarySharedProps & {\n fallback?: never;\n FallbackComponent?: never;\n fallbackRender: typeof FallbackRender;\n};\n\nexport type ErrorBoundaryPropsWithFallback = ErrorBoundarySharedProps & {\n fallback: ReactElement<\n unknown,\n string | FunctionComponent | typeof Component\n > | null;\n FallbackComponent?: never;\n fallbackRender?: never;\n};\n\nexport type ErrorBoundaryProps =\n | ErrorBoundaryPropsWithFallback\n | ErrorBoundaryPropsWithComponent\n | ErrorBoundaryPropsWithRender;\n"],"names":[],"version":3,"file":"react-error-boundary.module.js.map"}