Skip to article content
FrontendXLesson 10 / 40

Debugging, Error Boundaries and StrictMode

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

advanced · about 65 minutes · 4 exercises

  • Predict
  • Explain
  • Debug
  • Test

What you will be able to do

#
  • Diagnose common React bugs from symptoms.
  • Recover from render errors with an error boundary.
  • Use StrictMode and DevTools to find purity and cleanup problems.

Overview

#

A debugging method

Reproduce the bug, read the console and the component stack, form a hypothesis, and test the smallest change. React error messages include a component stack and often a link to the rule you broke. Read them before guessing.

Common bug classes

Stale state from closures: an event handler or effect captures an old value. Fix with updater functions or correct dependencies. Missing cleanup: subscriptions, timers and listeners leak and fire after unmount. Mutated state: React skips the update because the reference did not change. Unstable identity: a new object or function prop defeats memo and re-runs effects. Hydration mismatch: server HTML differs from the first client render, which React 19 reports with the differing markup.

Error boundaries

An error boundary is a class component that catches errors thrown during rendering, in lifecycle methods and in constructors of its descendants. It does not catch errors in event handlers, async code or itself.

snippet.tsx
class ErrorBoundary extends React.Component<
  { fallback: React.ReactNode; children: React.ReactNode },
  { hasError: boolean }
> {
  state = { hasError: false };
  static getDerivedStateFromError() {
    return { hasError: true };
  }
  componentDidCatch(error: Error, info: React.ErrorInfo) {
    console.error(error, info.componentStack);
  }
  render() {
    if (this.state.hasError) return this.props.fallback;
    return this.props.children;
  }
}

React 19 adds root options onCaughtError, onUncaughtError and onRecoverableError so you can route caught and uncaught errors to a logger.

StrictMode

StrictMode is a development tool. It double-invokes render, effects, reducers and state updaters to surface impurity and missing cleanup. It does not run in production. If double-invocation breaks your component, fix the component; do not remove StrictMode.

DevTools

The Components panel shows props, state and hooks for each component and lets you edit them live. The Profiler records renders and reasons. React 19 DevTools also surfaces Suspense and transition activity. Use them to confirm a hypothesis rather than reading code alone.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What happens when Child throws during render inside this boundary?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    class Boundary extends React.Component<{ children: React.ReactNode }, { hasError: boolean }> {
      state = { hasError: false };
      static getDerivedStateFromError() { return { hasError: true }; }
      render() {
        if (this.state.hasError) return <p>Something went wrong</p>;
        return this.props.children;
      }
    }
    
    <Boundary><Child /></Boundary>
    Choose the best answer
  2. Exercise 2Debug

    In development this effect logs twice on mount. Decide whether this is a bug and what to check.

    starter.tsx
    useEffect(() => {
      console.log("connected");
      const socket = connect();
      return () => socket.close();
    }, []);

    Reveal the reference answer
    reference.tsx
    This is expected under StrictMode: React mounts, unmounts and remounts to test cleanup, so the effect runs twice with one cleanup between. It is only a bug if the cleanup is missing or the effect is not idempotent. Confirm that socket.close runs between the two connects and that no duplicate connection remains.

    Authored expected output: Two logs with one cleanup between them; exactly one live connection.

    StrictMode double-invocation is a development check, not a production behavior. The correct response is to verify cleanup, not to remove StrictMode or add a guard that hides the real problem.

  3. Exercise 3Explain

    Explain why an error thrown in an onClick handler is not caught by an error boundary.

    starter.tsx
    <button onClick={() => { throw new Error("boom"); }}>Go</button>

    Reveal the reference answer
    reference.tsx
    Error boundaries catch errors during rendering, in lifecycle methods and in constructors. Event handlers run outside the render path, after commit, so an error thrown there is a normal JavaScript exception that propagates to the global error handler. Handle it with a try/catch in the handler or a window error listener, not a boundary.

    Authored expected output: A written explanation that event handlers run outside the render phase.

    Boundaries wrap the render and commit lifecycle of descendants. Handlers execute on user interaction, outside that lifecycle, so they fall through to normal error handling.

  4. Exercise 4Test

    Describe a test that proves an error boundary shows its fallback when a child throws.

    starter.tsx
    render(<Boundary><ThrowingChild /></Boundary>)

    Reveal the reference answer
    reference.tsx
    Render the boundary with a child that throws, suppress the expected console error, then assert that the fallback text is present and the child's content is absent.

    Authored expected output: Assertions: fallback text present, crashed child content absent.

    The test drives the real error path through render. Suppressing the console error keeps the test output clean while still asserting the recovery behavior.

Common misconceptions

#
  • Error boundaries catch all errors. They catch render, lifecycle and constructor errors in descendants, not event handler or async errors.
  • StrictMode causes bugs. It reveals bugs by double-invoking; the component is at fault.
  • A console warning is safe to ignore. React warnings usually indicate a real correctness problem.
  • Hydration mismatches are cosmetic. They force a client re-render and can break interactivity.

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.