react-error-boundary 4.0.5 → 4.0.7
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/dist/react-error-boundary.d.ts +2 -2
- package/dist/react-error-boundary.d.ts.map +1 -1
- package/dist/react-error-boundary.js +15 -14
- package/dist/react-error-boundary.js.map +1 -1
- package/dist/react-error-boundary.module.js +15 -14
- package/dist/react-error-boundary.module.js.map +1 -1
- package/package.json +6 -2
|
@@ -45,12 +45,12 @@ type ErrorBoundaryState = {
|
|
|
45
45
|
error: any;
|
|
46
46
|
};
|
|
47
47
|
export class ErrorBoundary extends Component<PropsWithRef<PropsWithChildren<ErrorBoundaryProps>>, ErrorBoundaryState> {
|
|
48
|
-
|
|
48
|
+
constructor(props: ErrorBoundaryProps);
|
|
49
49
|
static getDerivedStateFromError(error: Error): {
|
|
50
50
|
didCatch: boolean;
|
|
51
51
|
error: Error;
|
|
52
52
|
};
|
|
53
|
-
resetErrorBoundary
|
|
53
|
+
resetErrorBoundary(...args: any[]): void;
|
|
54
54
|
componentDidCatch(error: Error, info: ErrorInfo): void;
|
|
55
55
|
componentDidUpdate(prevProps: ErrorBoundaryProps, prevState: ErrorBoundaryState): void;
|
|
56
56
|
render(): ReactElement<any, string | import("react").JSXElementConstructor<any>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";AAEA,uCAAuC;IACrC,QAAQ,EAAE,OAAO,CAAC;IAClB,KAAK,EAAE,GAAG,CAAC;IACX,kBAAkB,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;CAC9C,CAAC;AAEF,OAAO,MAAM,8EACyC,CAAC;ACDvD,gCAAgC,KAAK,EAAE,aAAa,GAAG,SAAS,CAAC;AAEjE,4BAA4B;IAC1B,KAAK,EAAE,GAAG,CAAC;IACX,kBAAkB,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;CAC9C,CAAC;A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GAAG,gBAAgB,CAC9C,GAAG,IAAI,CAAC;IACT,iBAAiB,CAAC,EAAE,KAAK,CAAC;IAC1B,cAAc,CAAC,EAAE,KAAK,CAAC;CACxB,CAAC;AAEF,iCACI,8BAA8B,GAC9B,+BAA+B,GAC/B,4BAA4B,CAAC;ACrCjC,0BAA0B;IAAE,QAAQ,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,GAAG,CAAA;CAAE,CAAC;AAO5D,0BAA2B,SAAQ,UACjC,aAAa,kBAAkB,kBAAkB,CAAC,CAAC,EACnD,kBAAkB,CACnB;gBACa,KAAK,EAAE,kBAAkB;IAOrC,MAAM,CAAC,wBAAwB,CAAC,KAAK,EAAE,KAAK;;;;IAI5C,kBAAkB,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAajC,iBAAiB,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS;IAI/C,kBAAkB,CAChB,SAAS,EAAE,kBAAkB,EAC7B,SAAS,EAAE,kBAAkB;IAyB/B,MAAM;CAsCP;AEhHD,gCAAgC,KAAK,IAAI;IACvC,aAAa,EAAE,MAAM,IAAI,CAAC;IAC1B,YAAY,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACtC,CAAC;AAEF,iCAAiC,KAAK,GAAG,GAAG,KAAK,oBAAoB,KAAK,CAAC,CAiC1E;AC9BD,kCAAkC,KAAK,SAAS,MAAM,EACpD,SAAS,EAAE,cAAc,KAAK,CAAC,EAC/B,kBAAkB,EAAE,kBAAkB,GACrC,0BAA0B,gBAAgB,KAAK,CAAC,GAAG,cAAc,GAAG,CAAC,CAAC,CAexE","sources":["src/src/ErrorBoundaryContext.ts","src/src/types.ts","src/src/ErrorBoundary.ts","src/src/assertErrorBoundaryContext.ts","src/src/useErrorBoundary.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"}
|
|
@@ -37,12 +37,27 @@ const $6d6d4999e62b3ee0$var$initialState = {
|
|
|
37
37
|
error: null
|
|
38
38
|
};
|
|
39
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
|
+
}
|
|
40
45
|
static getDerivedStateFromError(error) {
|
|
41
46
|
return {
|
|
42
47
|
didCatch: true,
|
|
43
48
|
error: error
|
|
44
49
|
};
|
|
45
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
|
+
}
|
|
46
61
|
componentDidCatch(error, info) {
|
|
47
62
|
this.props.onError?.(error, info);
|
|
48
63
|
}
|
|
@@ -84,20 +99,6 @@ class $6d6d4999e62b3ee0$export$e926676385687eaf extends (0, $8zHUo$react.Compone
|
|
|
84
99
|
}
|
|
85
100
|
}, childToRender);
|
|
86
101
|
}
|
|
87
|
-
constructor(...args){
|
|
88
|
-
super(...args);
|
|
89
|
-
this.state = $6d6d4999e62b3ee0$var$initialState;
|
|
90
|
-
this.resetErrorBoundary = (...args)=>{
|
|
91
|
-
const { error: error } = this.state;
|
|
92
|
-
if (error !== null) {
|
|
93
|
-
this.props.onReset?.({
|
|
94
|
-
args: args,
|
|
95
|
-
reason: "imperative-api"
|
|
96
|
-
});
|
|
97
|
-
this.setState($6d6d4999e62b3ee0$var$initialState);
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
102
|
}
|
|
102
103
|
function $6d6d4999e62b3ee0$var$hasArrayChanged(a = [], b = []) {
|
|
103
104
|
return a.length !== b.length || a.some((item, index)=>!Object.is(item, b[index]));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AEQO,MAAM,4CACX,CAAA,GAAA,0BAAY,EAAmC;;;ADKjD,MAAM,qCAAmC;IACvC,UAAU;IACV,OAAO;AACT;AAEO,MAAM,kDAAsB,CAAA,GAAA,sBAAQ;IAMzC,OAAO,yBAAyB,KAAY,EAAE;QAC5C,OAAO;YAAE,UAAU;mBAAM;QAAM;IACjC;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,MAAM;gBACN,MAAM,UAAU;gBAChB,QAAQ;YACV;YAEA,IAAI,CAAC,SAAS;QAChB;IACF;IAEA,SAAS;QACP,MAAM,YAAE,SAAQ,kBAAE,eAAc,qBAAE,kBAAiB,YAAE,SAAQ,EAAE,GAC7D,IAAI,CAAC;QACP,MAAM,YAAE,SAAQ,SAAE,MAAK,EAAE,GAAG,IAAI,CAAC;QAEjC,IAAI,gBAAgB;QAEpB,IAAI,UAAU;YACZ,MAAM,QAAuB;uBAC3B;gBACA,oBAAoB,IAAI,CAAC;YAC3B;YAEA,IAAI,CAAA,GA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ACJ,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 state = initialState;\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
|
+
{"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;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AAU,EAAE,KAAK,CAAC,MAAM,QAAU,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,MAAM;AAE9E;;;;;;;;AGxHO,SAAS,0CACd,KAAU;IAEV,IACE,SAAS,QACT,OAAO,MAAM,aAAa,aAC1B,OAAO,MAAM,uBAAuB,YAEpC,MAAM,IAAI,MAAM;IAGlB,OAAO;AACT;;;;ADLO,SAAS;IACd,MAAM,UAAU,CAAA,GAAA,uBAAS,EAAE,CAAA,GAAA,yCAAmB;IAE9C,CAAA,GAAA,yCAAyB,EAAE;IAE3B,MAAM,CAAC,OAAO,SAAS,GAAG,CAAA,GAAA,qBAAO,EAG9B;QACD,OAAO;QACP,UAAU;IACZ;IAEA,MAAM,WAAW,CAAA,GAAA,oBAAM,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"}
|
|
@@ -21,12 +21,27 @@ const $44d7e150ebc754d2$var$initialState = {
|
|
|
21
21
|
error: null
|
|
22
22
|
};
|
|
23
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
|
+
}
|
|
24
29
|
static getDerivedStateFromError(error) {
|
|
25
30
|
return {
|
|
26
31
|
didCatch: true,
|
|
27
32
|
error: error
|
|
28
33
|
};
|
|
29
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
|
+
}
|
|
30
45
|
componentDidCatch(error, info) {
|
|
31
46
|
this.props.onError?.(error, info);
|
|
32
47
|
}
|
|
@@ -68,20 +83,6 @@ class $44d7e150ebc754d2$export$e926676385687eaf extends (0, $hgUW1$Component) {
|
|
|
68
83
|
}
|
|
69
84
|
}, childToRender);
|
|
70
85
|
}
|
|
71
|
-
constructor(...args){
|
|
72
|
-
super(...args);
|
|
73
|
-
this.state = $44d7e150ebc754d2$var$initialState;
|
|
74
|
-
this.resetErrorBoundary = (...args)=>{
|
|
75
|
-
const { error: error } = this.state;
|
|
76
|
-
if (error !== null) {
|
|
77
|
-
this.props.onReset?.({
|
|
78
|
-
args: args,
|
|
79
|
-
reason: "imperative-api"
|
|
80
|
-
});
|
|
81
|
-
this.setState($44d7e150ebc754d2$var$initialState);
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
86
|
}
|
|
86
87
|
function $44d7e150ebc754d2$var$hasArrayChanged(a = [], b = []) {
|
|
87
88
|
return a.length !== b.length || a.some((item, index)=>!Object.is(item, b[index]));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;AEQO,MAAM,4CACX,CAAA,GAAA,oBAAY,EAAmC;;;ADKjD,MAAM,qCAAmC;IACvC,UAAU;IACV,OAAO;AACT;AAEO,MAAM,kDAAsB,CAAA,GAAA,gBAAQ;IAMzC,OAAO,yBAAyB,KAAY,EAAE;QAC5C,OAAO;YAAE,UAAU;mBAAM;QAAM;IACjC;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,MAAM;gBACN,MAAM,UAAU;gBAChB,QAAQ;YACV;YAEA,IAAI,CAAC,SAAS;QAChB;IACF;IAEA,SAAS;QACP,MAAM,YAAE,SAAQ,kBAAE,eAAc,qBAAE,kBAAiB,YAAE,SAAQ,EAAE,GAC7D,IAAI,CAAC;QACP,MAAM,YAAE,SAAQ,SAAE,MAAK,EAAE,GAAG,IAAI,CAAC;QAEjC,IAAI,gBAAgB;QAEpB,IAAI,UAAU;YACZ,MAAM,QAAuB;uBAC3B;gBACA,oBAAoB,IAAI,CAAC;YAC3B;Y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A,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,iBAAS,EACvB,CAAC,OAAc,MACb,CAAA,GAAA,oBAAY,EACV,CAAA,GAAA,yCAAY,GACZ,oBACA,CAAA,GAAA,oBAAY,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 state = initialState;\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"}
|
|
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,EAAE;QACjC,MAAM,SAAE,MAAK,EAAE,GAAG,IAAI,CAAC;QAEvB,IAAI,UAAU,MAAM;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AAU,EAAE,KAAK,CAAC,MAAM,QAAU,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC,MAAM;AAE9E;;;;;;;;AGxHO,SAAS,0CACd,KAAU;IAEV,IACE,SAAS,QACT,OAAO,MAAM,aAAa,aAC1B,OAAO,MAAM,uBAAuB,YAEpC,MAAM,IAAI,MAAM;IAGlB,OAAO;AACT;;;;ADLO,SAAS;IACd,MAAM,UAAU,CAAA,GAAA,iBAAS,EAAE,CAAA,GAAA,yCAAmB;IAE9C,CAAA,GAAA,yCAAyB,EAAE;IAE3B,MAAM,CAAC,OAAO,SAAS,GAAG,CAAA,GAAA,eAAO,EAG9B;QACD,OAAO;QACP,UAAU;IACZ;IAEA,MAAM,WAAW,CAAA,GAAA,cAAM,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,iBAAS,EACvB,CAAC,OAAc,MACb,CAAA,GAAA,oBAAY,EACV,CAAA,GAAA,yCAAY,GACZ,oBACA,CAAA,GAAA,oBAAY,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.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-error-boundary",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.7",
|
|
4
4
|
"description": "Simple reusable React error boundary component",
|
|
5
5
|
"author": "Brian Vaughn <brian.david.vaughn@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"clear:builds": "rm -rf ./dist",
|
|
22
22
|
"clear:parcel-cache": "rm -rf ./.parcel-cache",
|
|
23
23
|
"clear:node_modules": "rm -rf ./node_modules",
|
|
24
|
-
"prerelease": "rm -rf ./.parcel-cache && parcel build
|
|
24
|
+
"prerelease": "rm -rf ./.parcel-cache && parcel build",
|
|
25
25
|
"prettier": "prettier --write \"**/*.{css,html,js,json,jsx,ts,tsx}\"",
|
|
26
26
|
"prettier:ci": "prettier --check \"**/*.{css,html,js,json,jsx,ts,tsx}\"",
|
|
27
27
|
"test": "jest",
|
|
@@ -33,9 +33,13 @@
|
|
|
33
33
|
"@babel/runtime": "^7.12.5"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
+
"@parcel/config-default": "^2.9.0",
|
|
36
37
|
"@parcel/core": "^2.9.0",
|
|
37
38
|
"@parcel/packager-ts": "^2.9.0",
|
|
39
|
+
"@parcel/plugin": "^2.9.0",
|
|
40
|
+
"@parcel/source-map": "^2.1.1",
|
|
38
41
|
"@parcel/transformer-typescript-types": "^2.9.0",
|
|
42
|
+
"@parcel/utils": "^2.9.0",
|
|
39
43
|
"@types/jest": "^26.0.15",
|
|
40
44
|
"@types/react": "^18",
|
|
41
45
|
"@types/react-dom": "^18",
|