Error boundaries with React
Tags: react, javascript, reactnative, debugging
Published: 2020-11-07 00:03:27

One thing React does well ( among other things ) is to show console error and warning messages, they point out meaningful stuff: bad practices, bugs ( or potential bugs), and recommendations. If we don't pay attention to them, they will somehow follow us to soon or later make their point, however, during my career, I have work in several React apps where opening the console means looking at a long list of yellow and red messages left by previous tasks.

One error message I confess had not paid the necessary attention to, it is the one bellow saying:

> Consider adding an error boundary to your tree to customize error handling behavior. Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4c97jawrlpikvtu8xeva.png)

This error can appear at different, hard to predict circumstances like data fetch error, bad rerender of a component, a throw newError exception coming from somewhere, among others. We sometimes don't know how our app is going to behave when unexpected events like this happen, that's why React offers this type of messages with even a [link](https://reactjs.org/docs/error-boundaries.html#gatsby-focus-wrapper) included that can guide us to offer a better UI experience for our users.

###Let's see the following [code](https://codesandbox.io/s/error-boundry-react-tyx45?file=/src/App.js)

I have this simple component called Title that throws an error exception if the props argument has not been included.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ejdalkjgqttvlzq7asns.png)

I'm planning to now pass it inside another component as a child:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/shcldo7so2l0quaujyn2.png)

As we can see, the title prop was not passed over to the Title component which is going to make our app break as soon we hover on the element, but the worst part is the user experience, the user won't see anything on the screen, like literally.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dpwpbcpdchdc9ie9mv5i.png)

To communicate this much better, we are going to grab the error boundaries example component from the React Js documentation which works perfectly for this problem.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/58vhd5pr5vxy98hbm58i.png)

It is a simple wrapper class component that updates a state error depending on if something doesn't work as expected by making an error state change from false to true, if indeed something has gone wrong, we can then display something to the screen like a text message for example. We can also console some error details which will help us tackle the issue better thanks to the componentDidCatch lifecycle method

> Note: There is no version of hooks for this one because the getDerivedStateFromError and componentDidCatch "old school" lifecycle do not have an equivalent effect yet, more info ​ [here](https://reactjs.org/docs/hooks-faq.html#do-hooks-cover-all-use-cases-for-classes) 

We will wrap it on top of our App component.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4r8mlwazsgrzw65162q0.png)

Now when the user hovers, we can show something to the screen instead of leaving them blank.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rxe9emh5p3embz2erux6.png)

But this could be even better, notice the Title component is the one that can potentially bring issues, we do not need to wrap the whole App component with the error boundary, we can move it closer down like:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xrj6etnb6xxigrfgx4nl.png)

We now have a part of the app displaying content while the other it's not, which might be better than showing one big error.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cy94b2zfvwtu8lrb2z7f.png)

Full code example [here](https://codesandbox.io/s/error-boundry-react-tyx45?file=/src/App.js)

## Conclusion

When building software, some unexpected things could happen in our apps, it is our responsibility to give users the best experience by providing minimal information about these events, an error boundary component could help us achieve this with not a lot of effort. Be also mindful of other React errors and warning displaying in the console, as they usually point at problems or potential ones.

Follow me on [Twitter](https://twitter.com/rolandobarbella) for more content




 ​ ​ ​ ​