react-error-boundary 4.0.10 → 4.0.12

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