An error boundary is a React component that catches JavaScript errors anywhere in its child component tree, logs those errors, and displays a fallback UI instead of the crashed component tree.
Click to reveal answer
intermediate
Which lifecycle method is used in class components to catch errors for error boundaries?
The lifecycle method static getDerivedStateFromError(error) is used to update state when an error is caught, and componentDidCatch(error, info) is used to log the error or perform side effects.
Click to reveal answer
intermediate
Can functional components be error boundaries in React 19+?
No, currently only class components can be error boundaries because error boundary methods rely on lifecycle methods not available in functional components.
Click to reveal answer
beginner
What happens to the UI when an error boundary catches an error?
The error boundary renders a fallback UI, which is a safe and user-friendly message or component, instead of the broken component tree that caused the error.
Click to reveal answer
beginner
Why should you use error boundaries in a React app?
Error boundaries prevent the entire React app from crashing due to errors in parts of the UI. They improve user experience by showing fallback UI and help developers catch and log errors.
Click to reveal answer
Which React component type can be an error boundary?
AStateless components
BFunctional components with hooks
CPure components
DClass components
✗ Incorrect
Only class components can be error boundaries because they support lifecycle methods like getDerivedStateFromError and componentDidCatch.
What method updates state when an error is caught in an error boundary?
AgetDerivedStateFromError
BcomponentDidCatch
CrenderError
DcomponentWillCatch
✗ Incorrect
getDerivedStateFromError is a static method that updates state to show fallback UI after an error.
What does an error boundary do when it catches an error?
AReloads the page
BLogs the error and shows fallback UI
CIgnores the error
DRestarts the app
✗ Incorrect
Error boundaries catch errors, log them, and render a fallback UI to keep the app running.
Can error boundaries catch errors in event handlers?
ANo, error boundaries do not catch errors in event handlers
BOnly in class components
COnly if wrapped in try-catch
DYes, always
✗ Incorrect
Error boundaries catch errors during rendering, lifecycle methods, and constructors, but not in event handlers.
What is a good fallback UI for an error boundary?
AA blank screen
BThe error stack trace
CA friendly message explaining something went wrong
DReload button only
✗ Incorrect
A friendly message helps users understand the app had a problem without showing confusing technical details.
Explain how error boundaries improve user experience in React apps.
Think about what happens when a part of the UI breaks and how error boundaries help.
You got /4 concepts.
Describe the lifecycle methods involved in creating an error boundary class component.
Focus on methods that catch errors and update UI.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of an Error Boundary in React?
easy
A. To catch JavaScript errors in child components and display a fallback UI
B. To manage state across multiple components
C. To optimize rendering performance
D. To handle user input validation
Solution
Step 1: Understand Error Boundary role
Error boundaries catch errors in the components below them in the tree to prevent the whole app from crashing.
Step 2: Identify the correct purpose
They show a fallback UI when an error happens, improving user experience.
Final Answer:
To catch JavaScript errors in child components and display a fallback UI -> Option A
Quick Check:
Error boundaries catch errors = C [OK]
Hint: Error boundaries catch errors and show fallback UI [OK]
Common Mistakes:
Confusing error boundaries with state management
Thinking error boundaries optimize rendering
Assuming error boundaries handle input validation
2. Which of the following is the correct way to define an Error Boundary component in React?
easy
A. A class component implementing componentDidCatch and getDerivedStateFromError
B. A functional component using useErrorBoundary hook
C. A class component implementing componentDidMount and shouldComponentUpdate
D. A functional component with useEffect to catch errors
Solution
Step 1: Recall Error Boundary requirements
Error boundaries must be class components that implement componentDidCatch and getDerivedStateFromError lifecycle methods.
Step 2: Identify correct lifecycle methods
Only A class component implementing componentDidCatch and getDerivedStateFromError lists these two required methods correctly.
Final Answer:
A class component implementing componentDidCatch and getDerivedStateFromError -> Option A
Quick Check:
Class + componentDidCatch + getDerivedStateFromError = A [OK]
Hint: Error boundaries are class components with special lifecycle methods [OK]
Common Mistakes:
Thinking functional components can be error boundaries
Confusing lifecycle methods needed for error boundaries
Using hooks that don't exist for error boundaries
3. Given this Error Boundary class component, what will be rendered if ChildComponent throws an error?