06 / 09

Explain the use of the componentDidCatch method.

componentDidCatch(error, info) This lifecycle method is invoked after an error has been thrown by a descendant component.

It receives two parameters:
  1. 1

    error - The error that was thrown.

  2. 2

    info - An object with a componentStack key containing information about which component threw the error.

  1. 1

    componentDidCatch() is called during the “commit” phase, so side-effects are permitted.

  2. 2

    It should be used for things like logging errors:

Difficulty: 5/10
Topics: error boundaries, fallback UI, error logging

Scenario Questions

0-2 years experience
  1. 1

    How would you add an error boundary around a component that fetches data, and what would you show to the user when an error occurs?

  2. 2

    What happens to the UI if a child component throws an error and you haven't implemented componentDidCatch?

2-5 years experience
  1. 1

    We have a third‑party chart library that sometimes throws during render. How would you use componentDidCatch to keep the page alive, and what trade‑offs would you consider for the fallback UI?

  2. 2

    During debugging you notice errors in a lazy‑loaded route aren't being caught. Why might componentDidCatch not fire there, and how would you fix it?

5-8 years experience
  1. 1

    In a large app with many nested error boundaries, how would you design a strategy to log errors from componentDidCatch to a central monitoring service without hurting performance?

  2. 2

    If a componentDidCatch boundary catches an error but the layout still looks broken, how would you diagnose and resolve the issue considering React's reconciliation process?

8+ years experience
  1. 1

    Our legacy codebase mixes class components with functional components using hooks. How would you plan a migration to a unified error‑handling approach that leverages componentDidCatch or its hook equivalents while keeping backward compatibility?

  2. 2

    When introducing a global error boundary across micro‑frontends, what architectural considerations arise regarding user experience, error reporting, and isolation of failures?

Follow-up Questions

  • What are the main limitations of componentDidCatch?
  • How would you test that your error boundary works as expected?
  • Can you describe how you would send the caught error to an external monitoring service?