react 错误边界_React中错误边界的简单指南

react 错误边界

In this article, you’ll learn about Error Boundaries via code snippets and interactive demos. If you’re still wondering what Error Boundaries are and how they work, this simple guide is for you! 🥅🐛

在本文中,您将通过代码片段和交互式演示来了解错误边界 。 如果您仍然想知道什么是错误边界以及它们如何工作,那么本简单指南非常适合您! 🥅🐛

Error Boundaries were introduced in React v16 as a way to catch tricky errors that occur during the render phase. In the past this would have caused the app to unmount completely, and the user would just see a blank web page, which is not ideal! 😱

在React v16中引入了错误边界,以捕获在渲染阶段发生的棘手错误。 在过去,这会导致应用程序完全卸载,而用户只会看到空白网页,这是不理想的! 😱

没有错误边界 (Without Error Boundaries)

It’s inevitable that we’ll encounter unexpected errors in our apps. You could be trying to access a deeply-nested property on an object that doesn’t exist, or sometimes it’s not in your control (like a failed HTTP request to a 3rd-party API).

不可避免的是,我们会在应用程序中遇到意外错误。 您可能试图访问一个不存在的对象上的深层属性,或者有时不在您的控件中(例如,对第三方API的HTTP请求失败)。

In the demo below, we’ll simulate an error to see what normally happens without an Error Boundary.

在下面的演示中,我们将模拟一个错误,以查看没有错误边界的情况下通常会发生什么。

Click the “+” button, and it will fail at 5:

单击“ +”按钮,它将在5失败:

See the Pen alligatorio-react-error-boundaries-1 by wle8300 (@wle8300) on CodePen.

请参阅CodePenwle8300 ( @ wle8300 )的Pen alligatorioio-react-error-boundaries-1

BuggyCounter.js
BuggyCounter.js

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值