Going Deeper: Production ReactExtra· 35 min read

Error Boundaries

Catch errors thrown while rendering and show a friendly fallback instead of a blank white screen.

What you will learn

  • Explain what an error boundary catches
  • Wrap part of the UI in a boundary with a fallback
  • Know what boundaries do not catch

The blank-white-screen problem

If a component throws an error while React is drawing it (say it reads user.name but user is null), React unmounts the whole app and the user sees a blank white screen. An error boundary is a special component that wraps part of your UI, catches such errors, and shows a fallback (a friendly “something went wrong” message) instead — the rest of the page keeps working.

Think of it like a circuit breaker in your house: when one appliance shorts out, the breaker trips for that circuit only, so the whole house does not go dark.

The easy way: react-error-boundary

You can write a boundary by hand (it must be a class component — the one place React still needs one), but almost everyone uses the tiny, well-known library react-error-boundary. Install it, then wrap any risky part of your app. This runs in a real project, so it is shown as code with its Output.

Install the helper library
npm install react-error-boundary
Wrap a risky component in an error boundary
import { ErrorBoundary } from 'react-error-boundary';

function Fallback({ error }) {
  return (
    <div role="alert" style={{ padding: 16, color: '#b91c1c' }}>
      <p>Something went wrong.</p>
      <pre>{error.message}</pre>
    </div>
  );
}

function App() {
  return (
    <ErrorBoundary FallbackComponent={Fallback}>
      <Profile />   {/* if Profile throws while rendering, Fallback shows */}
    </ErrorBoundary>
  );
}

Here is exactly what happens:

  1. We write a Fallback component — the friendly UI to show when something breaks. It receives the error that was thrown and prints error.message.
  2. We wrap the risky part — <Profile /> — in <ErrorBoundary FallbackComponent={Fallback}>. The FallbackComponent prop tells the boundary what to show on failure.
  3. Normally you just see Profile. But the moment Profile throws an error while rendering, the boundary catches it and swaps in Fallback instead — no blank screen, and the rest of App around the boundary is untouched.

Note: Output: When Profile renders normally, you see the profile. If Profile crashes (e.g. it reads a property of null), that area is replaced by a red box reading Something went wrong. with the error message underneath — the page does not go blank.

What error boundaries do NOT catch

Boundaries only catch errors thrown during rendering. They do not catch:

  • Errors inside event handlers (e.g. an onClick) — handle those with a normal try/catch.
  • Errors inside async code like a fetch .then or setTimeout — catch and store them in state.
  • Errors during server-side rendering (not relevant for a plain Vite app).

Tip: Place boundaries strategically: one near the top for a whole-app fallback, plus smaller ones around independent sections (a sidebar, a widget) so one broken widget does not take down the page.

Watch out: During development React still shows its red error overlay even with a boundary in place — that is expected. The boundary’s fallback is what your users see in the production build.

Q. What does an error boundary catch?

Answer: Error boundaries catch errors thrown during rendering (and lifecycle), showing a fallback. Event-handler and async errors need try/catch.

✍️ Practice

  1. Install react-error-boundary and wrap a component that deliberately throws, with a fallback that shows the message.
  2. Add a “Try again” button to your fallback using the library’s resetErrorBoundary prop (check the docs).

🏠 Homework

  1. Add a top-level error boundary to one of your earlier React apps and write two sentences on what the user now sees if a component crashes.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →