Error handling in React best practices

后端 未结 4 631
别跟我提以往
别跟我提以往 2020-12-13 13:15

When rendering a component in React (with many subcomponents) and a JS error is thrown for whatever reason, what\'s the best way to handle this? Sure I can catch the error

4条回答
  •  有刺的猬
    2020-12-13 13:47

    For React v16 and up, the recommended way is to use error boundaries. This page (short and good)from the React team explains the feature: https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html. It includes a link to a codepen demo as well.

提交回复
热议问题