react-error-boundary 4.0.11 → 4.0.13
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 +4 -4
- package/dist/declarations/src/ErrorBoundary.d.ts +4 -1
- package/dist/declarations/src/useErrorBoundary.d.ts +3 -3
- package/dist/declarations/src/withErrorBoundary.d.ts +1 -1
- package/dist/react-error-boundary.cjs.js +5 -6
- package/dist/react-error-boundary.development.cjs.js +5 -6
- package/dist/react-error-boundary.development.esm.js +6 -7
- package/dist/react-error-boundary.esm.js +6 -7
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -20,12 +20,12 @@ yarn add react-error-boundary
|
|
|
20
20
|
## API
|
|
21
21
|
|
|
22
22
|
### `ErrorBoundary` component
|
|
23
|
-
Wrap an `ErrorBoundary` around other React components to "catch" errors and render a fallback UI. The component supports several ways to render a fallback (shown below).
|
|
23
|
+
Wrap an `ErrorBoundary` component around other React components to "catch" errors and render a fallback UI. The component supports several ways to render a fallback (as shown below).
|
|
24
24
|
|
|
25
25
|
> **Note** `ErrorBoundary` is a _client_ component. You can only pass props to it that are serializeable or use it in files that have a `"use client";` directive.
|
|
26
26
|
|
|
27
27
|
#### `ErrorBoundary` with `fallback` prop
|
|
28
|
-
The simplest way to render a default "something went wrong" type error message.
|
|
28
|
+
The simplest way to render a default "something went wrong" type of error message.
|
|
29
29
|
```js
|
|
30
30
|
"use client";
|
|
31
31
|
|
|
@@ -142,7 +142,7 @@ function Example() {
|
|
|
142
142
|
```
|
|
143
143
|
|
|
144
144
|
#### Dismiss the nearest error boundary
|
|
145
|
-
A fallback component can use this hook to request the nearest error boundary retry the render that
|
|
145
|
+
A fallback component can use this hook to request the nearest error boundary retry the render that originally failed.
|
|
146
146
|
|
|
147
147
|
```js
|
|
148
148
|
"use client";
|
|
@@ -211,4 +211,4 @@ If using Yarn:
|
|
|
211
211
|
|
|
212
212
|
---
|
|
213
213
|
|
|
214
|
-
[This blog post](https://kentcdodds.com/blog/use-react-error-boundary-to-handle-errors-in-react) shows more examples of how this package can be used, although it was written for the [version 3 API](https://github.com/bvaughn/react-error-boundary/releases/tag/v3.1.4).
|
|
214
|
+
[This blog post](https://kentcdodds.com/blog/use-react-error-boundary-to-handle-errors-in-react) shows more examples of how this package can be used, although it was written for the [version 3 API](https://github.com/bvaughn/react-error-boundary/releases/tag/v3.1.4).
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { Component, ErrorInfo } from "react";
|
|
2
2
|
import { ErrorBoundaryProps } from "./types.js";
|
|
3
3
|
type ErrorBoundaryState = {
|
|
4
|
-
didCatch:
|
|
4
|
+
didCatch: true;
|
|
5
5
|
error: any;
|
|
6
|
+
} | {
|
|
7
|
+
didCatch: false;
|
|
8
|
+
error: null;
|
|
6
9
|
};
|
|
7
10
|
export declare class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
8
11
|
constructor(props: ErrorBoundaryProps);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export type UseErrorBoundaryApi<
|
|
1
|
+
export type UseErrorBoundaryApi<TError> = {
|
|
2
2
|
resetBoundary: () => void;
|
|
3
|
-
showBoundary: (error:
|
|
3
|
+
showBoundary: (error: TError) => void;
|
|
4
4
|
};
|
|
5
|
-
export declare function useErrorBoundary<
|
|
5
|
+
export declare function useErrorBoundary<TError = any>(): UseErrorBoundaryApi<TError>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { RefAttributes, ForwardRefExoticComponent, PropsWithoutRef, ComponentType } from "react";
|
|
2
2
|
import { ErrorBoundaryProps } from "./types.js";
|
|
3
|
-
export declare function withErrorBoundary<Props extends
|
|
3
|
+
export declare function withErrorBoundary<Props extends object>(component: ComponentType<Props>, errorBoundaryProps: ErrorBoundaryProps): ForwardRefExoticComponent<PropsWithoutRef<Props> & RefAttributes<any>>;
|
|
@@ -83,12 +83,12 @@ class ErrorBoundary extends react.Component {
|
|
|
83
83
|
error,
|
|
84
84
|
resetErrorBoundary: this.resetErrorBoundary
|
|
85
85
|
};
|
|
86
|
-
if (
|
|
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
93
|
throw error;
|
|
94
94
|
}
|
|
@@ -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
|
|
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
|
|
136
|
+
}), [context.resetErrorBoundary]);
|
|
138
137
|
if (state.hasError) {
|
|
139
138
|
throw state.error;
|
|
140
139
|
}
|
|
@@ -83,12 +83,12 @@ class ErrorBoundary extends react.Component {
|
|
|
83
83
|
error,
|
|
84
84
|
resetErrorBoundary: this.resetErrorBoundary
|
|
85
85
|
};
|
|
86
|
-
if (
|
|
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
93
|
{
|
|
94
94
|
console.error("react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop");
|
|
@@ -115,7 +115,6 @@ function assertErrorBoundaryContext(value) {
|
|
|
115
115
|
if (value == null || typeof value.didCatch !== "boolean" || typeof value.resetErrorBoundary !== "function") {
|
|
116
116
|
throw new Error("ErrorBoundaryContext not found");
|
|
117
117
|
}
|
|
118
|
-
return true;
|
|
119
118
|
}
|
|
120
119
|
|
|
121
120
|
function useErrorBoundary() {
|
|
@@ -127,7 +126,7 @@ function useErrorBoundary() {
|
|
|
127
126
|
});
|
|
128
127
|
const memoized = react.useMemo(() => ({
|
|
129
128
|
resetBoundary: () => {
|
|
130
|
-
context
|
|
129
|
+
context.resetErrorBoundary();
|
|
131
130
|
setState({
|
|
132
131
|
error: null,
|
|
133
132
|
hasError: false
|
|
@@ -137,7 +136,7 @@ function useErrorBoundary() {
|
|
|
137
136
|
error,
|
|
138
137
|
hasError: true
|
|
139
138
|
})
|
|
140
|
-
}), [context
|
|
139
|
+
}), [context.resetErrorBoundary]);
|
|
141
140
|
if (state.hasError) {
|
|
142
141
|
throw state.error;
|
|
143
142
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { createContext, Component,
|
|
2
|
+
import { createContext, Component, createElement, isValidElement, useContext, useState, useMemo, forwardRef } from 'react';
|
|
3
3
|
|
|
4
4
|
const ErrorBoundaryContext = createContext(null);
|
|
5
5
|
|
|
@@ -79,12 +79,12 @@ class ErrorBoundary extends Component {
|
|
|
79
79
|
error,
|
|
80
80
|
resetErrorBoundary: this.resetErrorBoundary
|
|
81
81
|
};
|
|
82
|
-
if (
|
|
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
89
|
{
|
|
90
90
|
console.error("react-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop");
|
|
@@ -111,7 +111,6 @@ function assertErrorBoundaryContext(value) {
|
|
|
111
111
|
if (value == null || typeof value.didCatch !== "boolean" || typeof value.resetErrorBoundary !== "function") {
|
|
112
112
|
throw new Error("ErrorBoundaryContext not found");
|
|
113
113
|
}
|
|
114
|
-
return true;
|
|
115
114
|
}
|
|
116
115
|
|
|
117
116
|
function useErrorBoundary() {
|
|
@@ -123,7 +122,7 @@ function useErrorBoundary() {
|
|
|
123
122
|
});
|
|
124
123
|
const memoized = useMemo(() => ({
|
|
125
124
|
resetBoundary: () => {
|
|
126
|
-
context
|
|
125
|
+
context.resetErrorBoundary();
|
|
127
126
|
setState({
|
|
128
127
|
error: null,
|
|
129
128
|
hasError: false
|
|
@@ -133,7 +132,7 @@ function useErrorBoundary() {
|
|
|
133
132
|
error,
|
|
134
133
|
hasError: true
|
|
135
134
|
})
|
|
136
|
-
}), [context
|
|
135
|
+
}), [context.resetErrorBoundary]);
|
|
137
136
|
if (state.hasError) {
|
|
138
137
|
throw state.error;
|
|
139
138
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { createContext, Component,
|
|
2
|
+
import { createContext, Component, createElement, isValidElement, useContext, useState, useMemo, forwardRef } from 'react';
|
|
3
3
|
|
|
4
4
|
const ErrorBoundaryContext = createContext(null);
|
|
5
5
|
|
|
@@ -79,12 +79,12 @@ class ErrorBoundary extends Component {
|
|
|
79
79
|
error,
|
|
80
80
|
resetErrorBoundary: this.resetErrorBoundary
|
|
81
81
|
};
|
|
82
|
-
if (
|
|
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
89
|
throw error;
|
|
90
90
|
}
|
|
@@ -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
|
|
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
|
|
132
|
+
}), [context.resetErrorBoundary]);
|
|
134
133
|
if (state.hasError) {
|
|
135
134
|
throw state.error;
|
|
136
135
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-error-boundary",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.13",
|
|
4
4
|
"description": "Simple reusable React error boundary component",
|
|
5
5
|
"author": "Brian Vaughn <brian.david.vaughn@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"clear:builds": "rm -rf ./dist",
|
|
45
45
|
"clear:node_modules": "rm -rf ./node_modules",
|
|
46
46
|
"prerelease": "preconstruct build",
|
|
47
|
-
"
|
|
48
|
-
"
|
|
47
|
+
"lint": "eslint .",
|
|
48
|
+
"lint:fix": "eslint . --fix",
|
|
49
49
|
"test": "jest",
|
|
50
50
|
"test:watch": "jest --watch",
|
|
51
51
|
"typescript": "tsc --noEmit",
|
|
@@ -58,12 +58,20 @@
|
|
|
58
58
|
"@babel/preset-env": "^7.22.5",
|
|
59
59
|
"@babel/preset-typescript": "^7.21.5",
|
|
60
60
|
"@preconstruct/cli": "^2.8.1",
|
|
61
|
+
"@types/assert": "^1.5.10",
|
|
61
62
|
"@types/jest": "^26.0.15",
|
|
62
63
|
"@types/react": "^18",
|
|
63
64
|
"@types/react-dom": "^18",
|
|
65
|
+
"@typescript-eslint/eslint-plugin": "^5.52.0",
|
|
66
|
+
"assert": "^2.0.0",
|
|
67
|
+
"eslint": "^8.0.1",
|
|
68
|
+
"eslint-config-prettier": "^9.0.0",
|
|
69
|
+
"eslint-plugin-import": "^2.25.2",
|
|
70
|
+
"eslint-plugin-prettier": "^5.0.0",
|
|
71
|
+
"eslint-plugin-react": "^7.33.1",
|
|
64
72
|
"jest": "^29.4.3",
|
|
65
73
|
"jest-environment-jsdom": "^29.4.3",
|
|
66
|
-
"prettier": "^
|
|
74
|
+
"prettier": "^3.0.1",
|
|
67
75
|
"react": "^18",
|
|
68
76
|
"react-dom": "^18",
|
|
69
77
|
"ts-jest": "^29.0.5",
|
|
@@ -72,14 +80,6 @@
|
|
|
72
80
|
"peerDependencies": {
|
|
73
81
|
"react": ">=16.13.1"
|
|
74
82
|
},
|
|
75
|
-
"eslintConfig": {
|
|
76
|
-
"extends": "./node_modules/kcd-scripts/eslint.js",
|
|
77
|
-
"rules": {
|
|
78
|
-
"react/prop-types": "off",
|
|
79
|
-
"react/no-did-update-set-state": "off",
|
|
80
|
-
"babel/no-unused-expressions": "off"
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
83
|
"preconstruct": {
|
|
84
84
|
"exports": {
|
|
85
85
|
"importConditionDefaultExport": "default"
|