Optimizing React Components with Error Boundaries
Tags: react, javascript, tutorials
Published: 2019-08-05 11:52:45

---
title: Optimizing React Components with Error Boundaries
published: true
description: Extending the error handling powers of your react application
tags: react, javascript, tutorials
cover_image: https://thepracticaldev.s3.amazonaws.com/i/92n4ig72853z1hiv7wik.png
---

>Disclaimer: The concept discussed in this article is part of the advanced concepts of React as outlined in the official docs. But I see it as something every react developer should know and use. You would find this my view to be accurate at the end of this tutorial.

#### The React we know: 
By its original design, when JavaScript errors occur in a component as little as a `Button` component, it leads to the complete crash and failure of the react application. At the time react did not provide a way to handle this, neither was it able to recover from these errors when they occurred.
 
But now, the React team at facebook with the advent of React 16, introduced a new concept for error handling called `error boundaries`.

#### Introducing Error Boundaries:
With the introduction of `error boundaries`, components can have errors and crash in peace without having to affect the entire application state causing an application-wide crash

According to the [React Documentation](https://reactjs.org/docs/error-boundaries.html), 

> 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.

Error boundaries work like the `catch(){}` block in JavaScript, but this time for components. Someone could say since it works like JavaScripts `catch(){}` block, why don't we use `try/catch` of creating error boundaries. So we could have something like this:
         
          try { 
             <ShowButton /> 
          } catch(err) {
             console.log(err)
          }
This looks good and could solve the problem we want to solve with `Error Boundaries`, but the thing is `try/catch` only works for imperative code, but since we are working with react, which is declarative, react components are declarative and specify what should be rendered. But with `Error Boundaries` the declarative nature of React is preserved.


>Just to add to this article, to understand the difference between imperative code and declarative code, look at these articles, [here](https://codeburst.io/declarative-vs-imperative-programming-a8a7c93d9ad2) and 
[here](https://medium.com/front-end-weekly/imperative-versus-declarative-code-whats-the-difference-adc7dd6c8380)


___

Error Boundaries, can only be class components, how do I mean, you can't use functional components, to create an `error boundary`, but a class component. 

For a class component to be considered an error boundary, it needs to have either (or both) of the lifecycle methods static `getDerivedStateFromError()` or `componentDidCatch()`. Where `getDerivedStateFromError()` is used to render a fallback UI with the error response the developer specifies, and `componentDidCatch()` is used to log error information, so here you could be using any log service of your choosing, or our favorite `console.log`.

___

To jump into code, this is what an error boundary looks like;

    
     import React, {Component} from 'react';
     class ErrorBoundary extends 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 };
        }

        componentDidCatch(error, info) {
           // You can also log the error to an error reporting service
           logErrorToMyService(error, info);
        }

        render() {
          if (this.state.hasError) {
            // You can render any custom fallback UI
            return <h1>Something went wrong.</h1>;
          }

          return this.props.children; 
        }
     }

>The code example was extracted from the react documentation, and for further reads, I would suggest you likely have a look at it.

With `Error boundaries` in place in our code, we won't have a mere error in our button component render, kill the entire application, we would have complete control over such things, and the users of the application won't be left wondering what happened. ![confused gif](https://media1.giphy.com/media/kaq6GnxDlJaBq/giphy.gif?cid=790b76115d480c6d6e704676777dfb3f&rid=giphy.gif)


To use the created `error boundary`, all we have to do is, wrap any component we want to be covered by error boundary within it as so;
 
  <ErrorBoundary>
      <MyButton />
  </ErrorBoundary>

Lastly, it's important to note that, error boundaries can't be used within event listeners. When dealing with event listeners, it's best to use the `try/catch` block.

> Note: How you want to use `error boundaries`, is totally up to you, you could have your entire application wrapped in an error boundary or simply have individual components wrapped within an error boundary. But, it is still highly recommended that you just put your <ErrorBoundary> in a few strategic places.


For further reads on error boundaries, I won't recommend other sources than the react documentation [here](https://reactjs.org/docs/error-boundaries.html)

I hope you enjoy using `error boundaries` as I do. That's it from me here, if you have any questions or feedback feel free to comment or DM on [twitter](https://twitter.com/devopsjay)