npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-error-catch

v1.0.19

Published

react error catch!

Downloads

56

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 来进行引入

紧接着在项目内:

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