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

How error boundaries catch errors

It appears that error boundaries are meant to catch runtime errors that prevent rendering to finish successfully. Hence, they are very specific to React and are implemented using a special life cycle hook of the class component.

Error boundaries do not catch errors for the following:

  • Event handlers
  • Asynchronous code (for example, setTimeout or requestAnimationFrame callbacks)
  • Server-side rendering
  • Errors thrown in the error boundary itself (rather than its children)

- React official documentation at https://reactjs.org/docs/error-boundaries.html.

Let's discuss the previously mentioned error boundaries limitations further:

  • Event handlers: This limitation is due to event handlers asynchronous nature. Callbacks are being invoked by an external function, and the event object is passed to a callback as a parameter. We do not have any control over this and when this will happen. The code is executed and never goes into the catch clause. Hint: This also impacts try-catch in the same way.
  • Asynchronous code: Most asynchronous code will not work with error boundaries. The exception to this rule is asynchronous render functions, which will come with future releases of React.
  • Server-side rendering: This usually concerns server-side rendered websites. Such websites are computed on the server and sent to the browser. Thanks to this, a user can immediately see the website's content. Most of the time, such server responses are cached and reused.
  • Errors thrown in the error boundary itself: You cannot catch errors that occur within the same class component. Hence, error boundaries should contain as little logic as possible. I always recommend using a separate component for them.
主站蜘蛛池模板: 富锦市| 白朗县| 清水河县| 嵊泗县| 葵青区| 班玛县| 揭东县| 巩义市| 克东县| 全椒县| 泌阳县| 江津市| 高州市| 塔河县| 安宁市| 玛多县| 多伦县| 南漳县| 忻州市| 新巴尔虎右旗| 太仓市| 保康县| 云浮市| 改则县| 寻甸| 淳安县| 柯坪县| 大名县| 花莲市| 新巴尔虎右旗| 南昌市| 温州市| 涞源县| 荃湾区| 南投县| 和平区| 阜平县| 碌曲县| 华池县| 神木县| 布尔津县|