Error boundaries are React class components that catch JavaScript errors in their child component tree during rendering, lifecycle methods, and constructors. They display a fallback UI instead of crashing the entire application.
An error boundary is a class component that implements getDerivedStateFromError, componentDidCatch, or both. When a child component throws during render, React catches the error and delegates it to the nearest boundary up the tree. The boundary then renders fallback UI instead of the broken subtree.
getDerivedStateFromError is a static method called during the render phase. It receives the error and returns an object to update state. componentDidCatch is called during the commit phase and receives both the error and an info object containing the component stack trace. Use it for side effects like logging.
Error boundaries only catch errors in the React render path: component render methods, lifecycle methods, and constructors. They do not catch errors in event handlers, asynchronous code (setTimeout, promises), server-side rendering, or errors thrown in the boundary itself.
Interview framing: define Error Boundaries in one sentence, then explain one concrete runtime behavior and one common pitfall with a short code example.