React - Error Boundaries

Jeremy Wood
3 min readOct 1, 2021

--

Photo by Michael Dziedzic on Unsplash

Do you have a React app? Does it crash? Would you like less of it to crash? Maybe you need Error Boundaries! They won’t fix the bugs you’ve coded in to your project, but they might keep a small mistake from triggering a large one.

Error boundaries do exactly what their name implies. They give you a way to section off parts of your code, and contain rendering, lifecycle, and construction errors to those individual blocks. They also give you the option of rendering failsafe UI, to keep the experience as pleasant as possible.

You do this by creating a class that extends React Component, add in logic for handling errors, and wrap your code to contain errors. Here is the official React example of an ErrorBoundary class.

Example of an Error Boundary class, from the official React documentation

Then, just wrap your chosen blocks in the ErrorBoundary class.

Basic React app structure with component VideoPlayer wrapped in an ErrorBoundary component

Normally, when a component on your page throws an error, nothing on the page is allowed to load. In this example, if there were an error in the video player, the main content would still be displayed. The VideoPlayer component itself would display whatever you specify in your ErrorBoundary class.

What blocks of code you wrap is up to you. You can wrap the whole application if you want, or section off into small blocks of code, whatever makes sense for your apps design. This can be a huge benefit if you want to dictate which clusters of components display one central error, and which have their own individual alerts.

There are some important points to keep in mind about this system. Errors bubble up. An error will move up from parent to parent until it hits an ErrorBoundary. This is what allows you to have one error handle multiple different components if need be. But keep in mind, an error on the same level as an ErrorBoundary, (sibling component), will not trigger that boundary. An error can only trigger a parent component.

There are also a couple places where an ErrorBoundary won’t work. Errors from inside event handlers are one example. When an event handler is running, (or failing), there is no rendering happening. This keeps the error from bubbling up to a parent component. Any server side errors, or asynchronous callback errors will be missed as well.

There are, of course, libraries that already have an implementation to get you started. One popular option is react-error-boundaries.

ErrorBoundary is an excellent approach to handling problems in your app. It’s certainly better than a blank page, and an opportunity to save your users from an experience they won’t forget.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Jeremy Wood
Jeremy Wood

Written by Jeremy Wood

I’m a full stack engineer who loves to learn, solve problems, and fix things! When I’m not working on my code, you’ll usually find me working on my motorcycles.

No responses yet

Write a response