Course contentsShow
← React
Lesson 1298 of 2,39633. Error BoundariesFree lesson

Error Boundaries Must Be Class Components

Why error boundaries haven't been converted to hooks yet, and the technical reasons React still requires classes.

Error Boundaries Must Be Class Components

What you'll learn: Why React hasn't converted error boundaries to hooks, and the technical reasons they still require class components.

The Surprising Class Requirement

Despite React's strong push toward functional components and hooks, error boundaries remain one of the few features that must be implemented as class components. There are currently no hooks for error boundaries—no useCatch or useErrorBoundary equivalent exists in React's core API.

Why Classes Are Still Required

The technical reason is straightforward: error boundaries need two lifecycle methods that have no functional equivalent:

  • static getDerivedStateFromError() — Called during the render phase to update state when an error is caught
  • componentDidCatch() — Called during the commit phase to log error information or trigger side effects

These lifecycle methods were specifically designed to handle errors at precise points in React's rendering process. The React team hasn't created hook equivalents because hooks themselves can throw errors that need to be caught. Creating a hook-based error boundary would create a circular dependency—what catches errors in the error-catching hook?

The Philosophical Challenge

Think of it like a safety net: the net itself can't be made of the same material it's designed to catch, because if that material fails, the whole system fails. Error boundaries need to operate at a lower, more stable level than the components they protect.

The Current State

While the React team has discussed hook-based error boundaries, they remain class-only as of React 18. Third-party libraries like react-error-boundary provide convenient wrappers, but under the hood, they still use class components.

Key Takeaway: Error boundaries must be class components because they require special lifecycle methods (getDerivedStateFromError and componentDidCatch) that have no hook equivalents—hooks themselves can throw errors, so they can't safely catch their own failures.