In client-side React applications, a JavaScript runtime error inside any component's render function (such as attempting to read a property of undefined: student.profile.avatar) corrupts React's internal Virtual DOM tree.
In standard React:
For users, a blank white screen looks like a complete system crash with zero explanation or recovery path.
An Error Boundary is a React component that catches JavaScript errors anywhere in its child component tree, logs the errors, and displays a friendly fallback UI instead of crashing the entire application.
Error Boundaries catch errors during:
As of React 18, Error Boundaries must be implemented as Class Components. There is currently no functional hook equivalent for componentDidCatch or getDerivedStateFromError.
An Error Boundary class defines one or both of these lifecycle methods:
static getDerivedStateFromError(error): Renders a fallback UI upon an error.componentDidCatch(error, errorInfo): Logs error information to an external telemetry service (e.g. Sentry, Datadog).Error Boundaries do NOT catch errors in:
onClick). Use standard try...catch blocks for event handlers.setTimeout, requestAnimationFrame, raw unhandled Promise rejections).Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Composing Multiple Hooks and Custom Hook Testing | Building Fallback UI with react-error-boundary |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.