# Error Boundaries

> How to stop a single crashed component from taking down your entire application.

Subject: React | Lesson 24 of 48 | 4 min read
Interactive lesson: https://codewithanimation.in/react/error-boundaries
Previous: [Custom Hooks](https://codewithanimation.in/react/custom-hooks.md) | Next: [Portals](https://codewithanimation.in/react/portals.md)

## The Catastrophic Failure Rule

If a React component throws a JavaScript error during rendering (like trying to access `user.name` when `user` is undefined), React has a strict rule: **it will unmount the entire component tree.**

This means a single broken profile picture can cause your entire app—including the header, navigation, and sidebar—to go completely blank (the 'White Screen of Death').

An Error Boundary is a special component that catches these errors, prevents the crash from bubbling up, and displays a fallback UI instead.

## Isolating Crashes

Think of Error Boundaries like try/catch blocks, but for UI.

If you wrap your `<Sidebar>` in an `<ErrorBoundary>`, and the sidebar crashes, only the sidebar is replaced with an error message. The rest of the app continues functioning perfectly.

## How to build one

Currently, React requires Error Boundaries to be written as **Class Components**, not functional components. (Though most people use the `react-error-boundary` library to avoid writing classes).

To make a class component an Error Boundary, you define either `static getDerivedStateFromError(error)` (to render a fallback UI) or `componentDidCatch(error, info)` (to log the error to a service like Sentry).

Then, you just wrap it around the parts of your app you want to protect: `<ErrorBoundary><Widget /></ErrorBoundary>`

## Common mistake: What they DON'T catch

Error boundaries **only catch errors that occur during rendering, in lifecycle methods, and in constructors.**

They do **NOT** catch errors in:

1. Event handlers (e.g., an error inside an `onClick` function).

2. Asynchronous code (e.g., a failed `fetch` or `setTimeout`).

3. Server-side rendering.

For event handlers and async code, you still need to use standard `try/catch` blocks.

## Tip: Where to place them

A common pattern is to have one top-level Error Boundary (to show a 'Something went wrong' page if everything fails), and then smaller Error Boundaries around independent widgets, route segments, or complex UI components.

## Try it: The Blast Radius

Toggle the Error Boundary on and off, then click 'Trigger Crash' in the deeply nested widget. Watch how the boundary stops the crash from destroying the entire app.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/error-boundaries

## Check yourself

### Question 1

What happens if a deeply nested component throws an error during render, and there are no Error Boundaries?

- A. React ignores the component and renders the rest of the app.
- B. React unmounts the entire component tree, resulting in a blank screen.
- C. React automatically displays a default error overlay to the user.

Correct answer: B

React's philosophy is that it is worse to leave corrupted UI in place than to remove it completely.

- Why not A: React does not fail silently. It removes the entire tree.
- Why not C: The error overlay is only in development mode. In production, it's just a blank screen.

### Question 2

Which of the following errors will an Error Boundary ACTUALLY catch?

- A. An error thrown inside a `fetch().then()` block.
- B. An error thrown inside an `onClick` event handler.
- C. An error thrown while calculating a variable inside the main body of a functional component.

Correct answer: C

Error boundaries specifically catch errors that occur during the *render phase* (when React is calculating the JSX).

- Why not A: Error boundaries do not catch asynchronous errors.
- Why not B: Error boundaries do not catch event handler errors. Use a try/catch block.

### Question 3

How do you write a custom Error Boundary from scratch in vanilla React?

- A. You must write it as a Class Component using `getDerivedStateFromError`.
- B. You use the `useErrorBoundary` hook.
- C. You wrap the component in `React.memo`.

Correct answer: A

As of React 18, Error Boundaries are the one remaining feature that strictly requires a Class Component implementation.

- Why not B: There is no built-in `useErrorBoundary` hook in React.
- Why not C: `React.memo` is for performance optimization, not error handling.

## Remember this

- Uncaught rendering errors unmount the entire React tree.
- Error boundaries catch rendering errors and display a fallback UI.
- They MUST be written as Class Components (or use a library).
- They do NOT catch errors in event handlers or async code.
- Wrap independent widgets in boundaries to isolate crashes.
