官术网_书友最值得收藏!

When to use error boundaries

ErrorBoundary is a great pattern for sure. It takes the try-catch concept into declarative JSX. The first time I saw it, I immediately came up with the idea to wrap the whole application into a boundary. This is fine, but it is not the only use case.

Consider the following use cases for error boundaries:

  • Widgets: Given some incorrect data, your widget may run into problems. If, in the worst case scenario, it cannot handle the data, it may throw an error. You will want the rest of the app to be usable, given that this widget is not crucial for the rest of the application. Your analytics code should collect the error and save at least a stack trace so that the developers can fix it.
  • Modals: Preserve the rest of the application from the faulty modal. These are usually meant to display some data and short messages. You do not want a modal to blow up your application. Such errors should be considered very rare, but better safe than sorry.
  • Boundaries on feature containers: Let's say that your app is divided into major features that are represented by container components. For example, let's take a messaging app such as Facebook Messenger. You may add error boundaries to the sidebar, my story bar, footer, start new message button, and messages history list view. This will ensure that, if one feature breaks, the others have a chance to still work properly.

Now we know about all of the pros, let's discuss the cons ones: Mixins.

主站蜘蛛池模板: 永宁县| 聊城市| 安达市| 天水市| 桂阳县| 荥阳市| 池州市| 五华县| 巨鹿县| 稻城县| 安福县| 山阳县| 商丘市| 沿河| 西平县| 精河县| 万安县| 中江县| 隆尧县| 沁源县| 五莲县| 错那县| 礼泉县| 卢龙县| 于都县| 彭山县| 通道| 宁河县| 河西区| 门头沟区| 武强县| 九江市| 金山区| 汶上县| 当雄县| 化隆| 无为县| 长岛县| 大连市| 堆龙德庆县| 广灵县|