Error Boundaries Explained (Simple Guide)

React Error Boundaries: what they solve (UI exceptions), what they don’t (non-UI errors), and how to use them in practice.

Oct 4, 2020 · 2 min read · 632 Words · -Views -Comments · Programming
React 前端开发 WebShell 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

React文档中讲到一个概念-Error Boundaries(错误边界),今天正好在读一本书《Learning React》,借着刚读完的熟悉感 + 结合实际的项目,聊下个人的实践认知。

What problems they solve

A JavaScript error in a part of the UI shouldn’t break the whole app. To solve this problem for React users, React 16 introduces a new concept of an “error boundary”.

如上为官网开篇的一段介绍,由此可以看出该技术的根本目标是希望UI层的异常不要导致整体的App的崩溃。

注意

  • 仅限UI层

  • 对于这种处理实际上,可以看作我们函数中经常写的try catch

  • React开发模式打包,出现了运行异常,在显示我们既定的错误边界render外,还是会展示堆栈错误信息

Out of scope

比如我当前的项目,技术栈为React+Redux+Redux-Saga+Axios等,假如是在Saga-effects中出现了未捕获的异常,实际上也会造成whole app的崩溃,Error Boundaries是无法解决这个问题的,因为不在UI层。那怎么办呢,就是针对性对saga要做try catch,比如做个高阶函数-safe-wrapper。

Practical usage

这里没什么好写的,官网例子足矣。当然需要注意的是,因为这种设计,实际上我们也可以很方便的捕获了UI层的错误,发送到issue📱服务上。对于Web产品运维,这还是很重要的。

Final Thoughts

在实际开发中,越来越喜欢React,因为足够的简单,足够的纯粹。但同时,在这个simple design的Lib下,也学到了很多,比如设计模式,比如编程范式等等。

References

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover