Error handling
Behaviour in React 15 and Earlier
In the past, JavaScript errors inside components used to corrupt React’s internal state and cause it to emit cryptic errors on subsequent renders. These errors were always caused by an earlier error in the application code, but React did not provide a way to handle them gracefully in components, and could not recover from them.
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 lifecycle method called componentDidCatch
, which allows to gracefully handle errors. This concept is called an “error boundary”.
Using Error Boundaries
Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
A class component becomes an error boundary if it defines a new lifecycle method called componentDidCatch(error, info)
:
Then you can use it as a regular component:
The componentDidCatch()
method works like a JavaScript catch {}
block, but for components. Only class components can be error boundaries. In practice, most of the time you’ll want to declare an error boundary component once and use it throughout your application.
Note that error boundaries only catch errors in the components below them in the tree. An error boundary can’t catch an error within itself. If an error boundary fails trying to render the error message, the error will propagate to the closest error boundary above it. This, too, is similar to how catch {}
block works in JavaScript.
Where to Place Error Boundaries
The granularity of error boundaries is up to you. You may wrap top-level route components to display a “Something went wrong” message to the user, just like server-side frameworks often handle crashes. You may also wrap individual widgets in an error boundary to protect them from crashing the rest of the application.
Uncaught Errors
As of React 16, __errors that were not caught by any error boundary will result in unmounting of the whole React component tree.__
Prior to React 16, uncaught errors would leave the corrupted UI in place and the app would keep functioning. This has dangerous implications, as leaving a corrupted UI could for example lead to displaying the wrong amount for a payments app.
Last updated