04 Error Boundaries

错误边界

为什么需要?

在React16之前,组件内的JavaScript错误会导致React的内部状态被破坏,并且在下一次渲染时产生无法追踪的错误。这些错误基本上是由其他的非React组件代码错误引起的。但React并没有提供一种优雅的错误处理,也无法从错误中恢复。

部分UI的JS错误不应该导致整个应用的崩溃,为了解决这个问题,React引入了一个新的概念——错误边界。

错误边界是一种React组件,这种组件可以捕获并打印发生在其子组件数任何位置的JS错误,然后,渲染出备用UI,而非崩溃了的子组件。错误边界可以在渲染期间、生命周期方法和整个组件树的构造函数中捕获错误。

注意
错误边界无法捕获以下场景中产生的错误

  • 事件处理
  • 异步代码
  • 服务端渲染
  • 自身错误

怎样创建一个错误边界组件?

  1. 创建一个Class组件(只可以是Class组件)
  2. 在Class组件中定义static getDerivedStateFromError()componentDidCatch()这两个声明周期方法中的任意一个/两个,那么这个组件就是一个错误边界组件了。
class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props)
    this.state = { hasError: false }
  }

  static getDerivedStateFromError(error) {
    // 更新 state 使下一次渲染显示降级后UI
    return { hasError: true}
  }

  componentDidCatch(error, errorInfo) {
    // 可以将错误信息上报给服务器
    logErrorToServer(error,errorInfo)
  }

  render() {
    if(this.state.hasError) {
      return <h1>渲染降级后的UI界面</h1>
    }
    return this.props.children
  }
}

抛出错误后,使用getDerivedStateFromError()渲染备用UI,使用componentDidCatch()打印错误信息

然后你就可以将它作为一个常规组件来使用了

<ErrorBoundary>
  <MyWidget />
</ErrorBoundary>

任何未被错误边界捕获的错误将会导致组件树被卸载,移除好过错误。


错误边界应该放在哪儿?

错误边界的粒度是开发者决定的,你可以将错误边界放在最顶层,也可以用于捕获特定的组件错误,以保护其他部分不崩溃。


错误边界与try…catch

异同点:

  • try/catch是命令式代码(imperative code)
  • 错误组件是(React组件)是声明式的
  • 错误边界只针对React组件,只有class组件可以成为错误边界组件
  • 错误边界只能捕获子组件错误,若不能渲染错误,则错误会冒泡至最近的上层错误边界,与catch类似

关于事件处理

错误边界是无法捕获事件处理器内部错误的。
React也不需要错误边界来捕获时间处理器内部的错误。与render方法和生命周期方法不同,事件处理函数不会在渲染期间触发,因此如果你需要在事件处理器内部捕获错误,直接使用不同的try/catch语句就可以了。


本节示例程序见: myapp>src>components>ErrorBoundary

注意:

  • 错误边界的演示示例必须在生产环境下测试,生产环境下Webpack会使页面跳转到错误蒙版页面
  • 因此需要编译打包后放入生产环境
  • 再使用前端静态服务器serve部署后测试
npm run build
npm install -g serve
serve -s build
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
There are several reasons why you might encounter detection errors when using SSD (Single Shot MultiBox Detector) for object detection. Here are a few possible causes and solutions: 1. Insufficient training data: SSD relies heavily on training data to learn how to detect objects. If your training data is insufficient, you may encounter detection errors. Try to gather more training data or use pre-trained models to improve your results. 2. Incorrect model configuration: Make sure that your model is properly configured, including the number of layers, the size of the input image, and the number of classes you are trying to detect. Adjusting these parameters can significantly affect the accuracy of your model. 3. Poor quality images: Low-quality images can cause detection errors, especially if the objects you are trying to detect are small or blurry. Try to use high-quality images with good lighting and clear object boundaries. 4. Insufficient model training: If your model is not adequately trained, it may not be able to accurately detect objects. Ensure that you train your model for a sufficient number of epochs and with enough data. 5. Overfitting: Overfitting occurs when your model becomes too specialized to the training data and is not able to generalize well to new data. This can result in detection errors. To prevent overfitting, try to use data augmentation techniques, such as random cropping and flipping, and use regularization techniques such as dropout. 6. Incorrect anchor box settings: SSD uses anchor boxes to detect objects at different scales and aspect ratios. If your anchor box settings are incorrect, you may encounter detection errors. Make sure that your anchor boxes cover the range of object sizes and aspect ratios that you are trying to detect. 7. Insufficient non-maximum suppression (NMS) threshold: NMS is used to remove duplicate detections. If your NMS threshold is too low, you may end up with multiple detections of the same object, which can cause errors. Adjusting the NMS threshold can help to reduce detection errors.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值