Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(*): rename 'info' parameter to 'errorInfo' in onError and componentDidCatch #206

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,29 @@ yarn add react-error-boundary
## API

### `ErrorBoundary` component

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).

> **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.

#### `ErrorBoundary` with `fallback` prop

The simplest way to render a default "something went wrong" type of error message.

```js
"use client";

import { ErrorBoundary } from "react-error-boundary";

<ErrorBoundary fallback={<div>Something went wrong</div>}>
<ExampleApplication />
</ErrorBoundary>
</ErrorBoundary>;
```

#### `ErrorBoundary` with `fallbackRender` prop

["Render prop"](https://react.dev/reference/react/Children#calling-a-render-prop-to-customize-rendering) function responsible for returning a fallback UI based on a thrown value.

```js
"use client";

Expand All @@ -62,8 +68,11 @@ function fallbackRender({ error, resetErrorBoundary }) {
<ExampleApplication />
</ErrorBoundary>;
```

#### `ErrorBoundary` with `FallbackComponent` prop

React component responsible for returning a fallback UI based on a thrown value.

```js
"use client";

Expand Down Expand Up @@ -97,7 +106,7 @@ function Fallback({ error, resetErrorBoundary }) {

import { ErrorBoundary } from "react-error-boundary";

const logError = (error: Error, info: { componentStack: string }) => {
const logError = (error: Error, errorInfo: { componentStack: string }) => {
// Do something with the error, e.g. log to an external API
};

Expand All @@ -109,6 +118,7 @@ const ui = (
```

### `useErrorBoundary` hook

Convenience hook for imperatively showing or dismissing error boundaries.

#### Show the nearest error boundary from an event handler
Expand All @@ -127,10 +137,10 @@ function Example() {

useEffect(() => {
fetchGreeting(name).then(
response => {
(response) => {
// Set data in state and re-render
},
error => {
(error) => {
// Show error boundary
showBoundary(error);
}
Expand All @@ -142,6 +152,7 @@ function Example() {
```

#### Dismiss the nearest error boundary

A fallback component can use this hook to request the nearest error boundary retry the render that originally failed.

```js
Expand All @@ -163,31 +174,35 @@ function ErrorFallback({ error }) {
```

### `withErrorBoundary` HOC

This package can also be used as a [higher-order component](https://legacy.reactjs.org/docs/higher-order-components.html) that accepts all of the same props as above:

```js
"use client";

import {withErrorBoundary} from 'react-error-boundary'
import { withErrorBoundary } from "react-error-boundary";

const ComponentWithErrorBoundary = withErrorBoundary(ExampleComponent, {
fallback: <div>Something went wrong</div>,
onError(error, info) {
onError(error, errorInfo) {
// Do something with the error
// E.g. log to an error logging client here
},
})
});

// Can be rendered as <ComponentWithErrorBoundary {...props} />
```

---

# FAQ

## `ErrorBoundary` cannot be used as a JSX component

This error can be caused by a version mismatch between [react](https://npmjs.com/package/react) and [@types/react](https://npmjs.com/package/@types/react). To fix this, ensure that both match exactly, e.g.:

If using NPM:

```json
{
...
Expand All @@ -199,6 +214,7 @@ If using NPM:
```

If using Yarn:

```json
{
...
Expand Down
4 changes: 2 additions & 2 deletions src/ErrorBoundary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class ErrorBoundary extends Component<
}
}

componentDidCatch(error: Error, info: ErrorInfo) {
this.props.onError?.(error, info);
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
this.props.onError?.(error, errorInfo);
}

componentDidUpdate(
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type FallbackProps = {
};

type ErrorBoundarySharedProps = PropsWithChildren<{
onError?: (error: Error, info: ErrorInfo) => void;
onError?: (error: Error, errorInfo: ErrorInfo) => void;
onReset?: (
details:
| { reason: "imperative-api"; args: any[] }
Expand Down