mediumFrontend

What are Error Boundaries in React?

267 views
01

Understand the problem

Describe what Error Boundaries are, how they work, and their utility in React applications.

error handlingerror boundariesuxlifecycle
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

A reusable Error Boundary
import { Component } from "react";

class ErrorBoundary extends Component {
  state = { hasError: false };
  static getDerivedStateFromError() { return { hasError: true }; } // show fallback
  componentDidCatch(error, info) { /* log to Sentry, etc. */ }
  render() {
    if (this.state.hasError) return <p>⚠️ Something went wrong.</p>;
    return this.props.children;
  }
}

function Broken() { throw new Error("render crash"); }

export default function App() {
  return (
    <div style={{ padding: 24, fontFamily: "system-ui" }}>
      <p>App still runs ↓</p>
      <ErrorBoundary><Broken /></ErrorBoundary>
    </div>
  );
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.