Error Boundary
Question
What is an error boundary in React?
Hint
Error boundaries use componentDidCatch and getDerivedStateFromError.
Explanation
Error boundaries are React class components that catch JavaScript errors in child components during rendering, lifecycle, and constructors. They use componentDidCatch(error, info) for side effects (logging) and getDerivedStateFromError(error) to update state. When an error is caught, the boundary renders a fallback UI. Currently, error boundaries must be class components — there is no hook equivalent. They do not catch errors in event handlers, async code, or setTimeout callbacks.
Related Resources
Related Lessons
- Error Boundaries
Learn error boundaries.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.