06 / 09

Explain the use of the componentDidCatch method.

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

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:

Scenario Questions

0-2 years experience

  1. 1How 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. 2What happens to the UI if a child component throws an error and you haven't implemented componentDidCatch?

2-5 years experience

  1. 1We 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. 2During 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. 1In 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. 2If 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. 1Our 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. 2When 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?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.