Error Boundaries in React: Catching Errors for a Better User Experience
React is a popular JavaScript library for building user interfaces. One of the challenges of building complex applications with React is handling unexpected errors that occur during rendering. When an error occurs, it can crash your entire application and leave your users confused and frustrated. This is where React Error Boundaries come in.
What are Error Boundaries?
Error Boundaries are React components that catch and handle errors that occur during rendering. They provide a way to recover from unexpected errors and display a fallback UI instead of crashing the entire application.
By default, if an error occurs in a React component's lifecycle method (e.g. componentDidMount
, render
, etc.) or in its descendant components, the entire component tree will unmount and display an error message in the console. Error Boundaries prevent this by catching the error and displaying a fallback UI instead.
How do Error Boundaries work?
Error Boundaries are special components in React that catch errors thrown by their child components during rendering. When an error is caught, the Error Boundary component replaces the child component with a fallback UI. The fallback UI can be any React component that you choose, such as an error message, a spinner, or a retry button.
Creating an Error Boundary
Creating an Error Boundary component in React is easy. You simply need to create a new class component and define a new method called static getDerivedStateFromError()
.
Here's an example of an Error Boundary component:
import React from 'react';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI here
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
In this example, we're using the getDerivedStateFromError
method to update the component's state when an error occurs. If an error occurs, the Error Boundary will render a custom fallback UI instead of the child component that threw the error.
Using Error Boundaries in Your React application
To use an Error Boundary in your React application, simply wrap the component tree that you want to protect with the Error Boundary component:
<ErrorBoundary>
<MyComponent />
</ErrorBoundary>
In this example, the MyComponent
component is wrapped with the ErrorBoundary
component. If an error occurs in MyComponent
or any of its descendant components, the ErrorBoundary
component will catch the error and display a fallback UI instead.
Types of Errors Caught by Error Boundaries
Error Boundaries in React are designed to catch and handle errors that occur during rendering, specifically errors thrown within their child components' lifecycle methods (e.g. componentDidMount
, render
, etc.) or in their descendant components.
The following types of errors are typically caught by Error Boundaries:
JavaScript errors thrown during rendering, such as a
TypeError
orReferenceError
.Errors thrown in a child component's lifecycle method such as
componentDidMount
orcomponentDidUpdate
.Errors thrown in event handlers or callbacks passed to a child component.
It's important to note that Error Boundaries do not catch errors that occur during server-side rendering or errors thrown in event handlers outside of the component tree.
Additionally, it's worth noting that React's Error Boundaries are not a replacement for server-side error handling or thorough testing practices. They're just a tool to help catch errors and provide a better user experience in the event of an unexpected error.
Conclusion
React Error Boundaries are an important tool for building robust and error-resistant applications. They allow you to catch and handle errors. To learn more about Error Boundary visit the Official React Docs.
Subscribe to my newsletter
Read articles from Manas Joshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by