React Error Boundaries Explained
Michael Mitrakos
4 min read
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach…
React Error Boundaries Explained
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
Error boundaries are a feature in React that allows you to catch and handle runtime errors in your application, preventing the entire application from crashing. This is especially useful for large applications where a single error could cause the entire application to break, leading to a poor user experience.
To create an error boundary, you can use a class component and add a new lifecycle method called `componentDidCatch```javascript . This method takes two arguments: an error object and an error information object. The error object contains the actual error that occurred, and the error information object contains details about where the error occurred in the component tree. Here’s an example of how you can create an error boundary in a class component:
class MyErrorBoundary extends React.Component {super(props);
this.state = { hasError: false };
}
this.setState({ hasError: true });
}
if (this.state.hasError) {
}
}
`<MyErrorBoundary>`
`<MyComponent />`
`<MyOtherComponent />`
</MyErrorBoundary>Now, if an error occurs in either `**MyComponent**` or `**MyOtherComponent**`, the `**componentDidCatch**` method in `**MyErrorBoundary**` will be invoked, and the `**hasError**` state will be set to `**true**`. This will cause the error boundary to render the fallback UI instead of the original component tree.
It’s important to note that error boundaries only catch errors that occur within the boundary itself. If an error occurs in a child component of an error boundary, the boundary will catch it and handle it. However, if an error occurs in a parent component of the boundary, the boundary will not catch it.
You can also use error boundaries to catch errors that occur during the rendering of a component. To do this, you can use a `**try-catch**` block in the `**render**` method of your error boundary. For example:
try {
return this.props.children;
} catch (error) {
this.setState({ hasError: true });
return <h1>Something went wrong.</h1>;
}
}
In addition to catching errors, error boundaries also provide a way to handle errors in a way that is more user-friendly. For example, you might want to display a loading spinner while the error is being handled, or you might want to display a message to the user explaining what went wrong.
Error boundaries are a powerful tool for handling errors in React applications, and they can help improve the user experience by preventing the entire application from crashing due to a single error. By wrapping your component tree in an error boundary, you can catch and handle errors in a way that is more graceful and user-friendly.
#### JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. [Check out the ebook now](https://www.mitrakos.com/ebook)!
I founded [Higglo Digital](https://higglo.io) and we can help your business crush the web game with an award-winning website and cutting-edge digital strategy. If you want to see a *beautifully designed website*, [check us out](https://higglo.io).
I also created [Wanderlust Extension](http://www.wanderlustapp.io/) to discover the most beautiful places across the world with highly curated content. Check it out!