# react-error-catch

> react error catch!

Latest version **1.0.23** (published 2024-12-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-error-catch
pnpm add react-error-catch
yarn add react-error-catch
bun add react-error-catch
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.23 |
| Published | 2024-12-22 |
| First published | 2021-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 17 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | xian_107 |
| Maintainers | xian_107 |
| Keywords | ["react", "error", "catch"] |

## Links

- npm: https://www.npmjs.com/package/react-error-catch
- Repository: https://github.com/xian107/react-error-catch
- Homepage: https://github.com/xian107/react-error-catch#readme
- Issues: https://github.com/xian107/react-error-catch/issues
- npm.io page: https://npm.io/package/react-error-catch

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 1.0.23 (latest) — 2024-12-22
- 1.0.22 — 2024-12-22
- 1.0.21 — 2024-12-22
- 1.0.20 — 2024-12-22
- 1.0.19 — 2024-09-12
- 1.0.18 — 2024-09-12
- 1.0.17 — 2024-09-12
- 1.0.15 — 2023-12-13
- 1.0.16 — 2023-08-15
- 1.0.13 — 2022-04-14
- 1.0.12 — 2021-05-18
- 1.0.11 — 2021-05-14
- 1.0.10 — 2021-05-14
- 1.0.9 — 2021-05-13
- 1.0.8 — 2021-05-13
- … 7 more at https://npm.io/package/react-error-catch/versions

## README

### 捕获 React 异常

| 异常类型                    | 同步方法 | 异步方法 | 资源加载 | Promise | async/await |
| :-------------------------- | :------- | :------- | :------- | :------ | :---------- |
| try/catch                   | √        |          |          |         | √           |
| onerror                     | √        | √        |          |         |             |
| error 事件监听              | √        | √        | √        |         |             |
| unhandledrejection 事件监听 |          |          |          | √       | √           |

错误表现类型：

- 渲染错误，渲染阶段发生异常，比如某个组件没有引入就使用
- 引用错误，即引入某个资源文件时发生错误，这个往往在编译过程中能够捕获到。在这里我们讨论异步引入的情况
- 事件处理，即渲染没问题，但是在在调用其触发事件时发生错误，比如 JSON.parse(JSON.parse("error"))，这类错误可以细分为用户手动触发和脚本触发，可以参考 Error.isTrusted 属性进行理解
- 异步代码，比如 promise.reject("error")

### 使用

执行 `npm install react-error-catch` 或者 `yarn add react-error-catch` 来进行引入

紧接着在项目内：

```javascript
import ErrorCatch from "react-error-catch";

const App = () => {
  return (
    <ErrorCatch
      app="react-catch"
      user="bcyg"
      delay={5000}
      max={1}
      filters={[]}
      onCatch={(errors) => {
        console.log("报错咯");
        // 上报异常信息到后端，动态创建标签方式
        new Image().src = `http://localhost:3000/log/report?info=${JSON.stringify(
          errors
        )}`;
      }}
    >
      <Main />
    </ErrorCatch>
  );
};

export default App;
```

## Props

| 属性        | 描述                                                 | 类型                        | 默认值                           |
| :---------- | :--------------------------------------------------- | :-------------------------- | :------------------------------- |
| errorRender | 当捕获到组件渲染错误时，降级渲染样式                 | React.ReactNode             | \<h1>Something went wrong.\</h1> |
| user        | 谁触发了错误                                         | "unkonwn user"              |                                  |
| app         | 触发错误的 app                                       | string                      | "unkonwn app"                    |
| token       | 触发错误的 token                                     | string                      | -                                |
| language    | 访问网站语言 language                                | string                      | -                                |
| max         | 当捕获到的错误超过设置 max 值时，触发 `onCatch` 事件 | number                      | 1                                |
| filters     | 定义需要过滤的错误                                   | string[]                    | -                                |
| onCatch     | 当满足设置条件时的错误捕获回调                       | (error: ErrorInfo[]) => any | -                                |

---
_Source: https://npm.io/package/react-error-catch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
