Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 13 / Topic 2 of 5

Writing a class-based boundary - the one class this course still asks you to write

Stage 2's legacy-reading note says plainly that no lesson time is spent writing class components. This section is the one deliberate exception, and it is worth confirming why rather than taking it on faith: as of this repo's own installed React (19.3.1, checked directly in node_modules/react-dom while writing this part), there is still no hook or function-component way to define an error boundary. getDerivedStateFromError and componentDidCatch are lifecycle methods React's reconciler calls on a class instance when a descendant throws during render - neither has ever shipped a hook equivalent, and nothing in React 19's hook additions (use, useActionState, useOptimistic) changes that. React 19 did add two root-level options, onCaughtError/onUncaughtError (passed to createRoot, or to Testing Library's render(..., { onCaughtError }) - verified directly against this repo's installed @testing-library/react), but those are for centralized error reporting across an entire app, not a substitute for the boundary itself - they fire alongside a boundary's own componentDidCatch, not instead of it.

examples/02_class_error_boundary_with_fallback.jsx's ErrorBoundary is the real, reusable version the rest of this part builds on:

// examples/02_class_error_boundary_with_fallback.jsx
export class ErrorBoundary extends Component {
  state = { error: null };

  static getDerivedStateFromError(error) {
    return { error };
  }

  componentDidCatch(error, info) {
    this.props.onError?.(error, info);
  }

  resetBoundary = () => { this.setState({ error: null }); };

  render() {
    if (this.state.error) return this.props.fallback(this.state.error, this.resetBoundary);
    return this.props.children;
  }
}

The two methods have different jobs. getDerivedStateFromError runs during the render phase and must be pure - its only job is returning the state the fallback needs, and React may call it more than once for the same real error. componentDidCatch runs during the commit phase, exactly once per real error, with a second argument whose info.componentStack names every component between the boundary and the one that actually threw - this is the one place to call an error-tracking service, never getDerivedStateFromError.

fallback is a function, not a plain element, specifically so it can show the real message and hand back a working reset:

// examples/02_class_error_boundary_with_fallback.jsx
<ErrorBoundary
  fallback={(error, reset) => (
    <div role="alert">
      <p>Could not show this order: {error.message}</p>
      <button onClick={reset}>Try again</button>
    </div>
  )}
>
  <LiveOrderWidget order={order} />
</ErrorBoundary>

Rendering CrashProofOrderSection with a cancelled order really shows that exact fallback text and a working onError callback carrying the component stack (examples/02_class_error_boundary_with_fallback.test.jsx) - and clicking "Try again" on that same still-cancelled order really crashes right back into the fallback immediately. That is not a bug in the boundary; it is the entire reason Section 3 exists.

Done here? The practice set for Part 13 — including the solutions — is on the part page.

Search the guide