Challenge - 5 Problems
Error Boundary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a child component throws an error inside an Error Boundary?
Consider a React app where a child component throws an error during rendering. The parent component uses an Error Boundary. What will be the rendered output?
React
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError() { return { hasError: true }; } render() { if (this.state.hasError) return <div>Something went wrong.</div>; return this.props.children; } } function Buggy() { throw new Error('Crash!'); return <div>All good</div>; } function App() { return <ErrorBoundary><Buggy /></ErrorBoundary>; }
Attempts:
2 left
💡 Hint
Error Boundaries catch errors in lifecycle methods and render phase, but only in class components.
✗ Incorrect
In React, Error Boundaries must be class components to catch errors during rendering. Functional components with hooks cannot catch errors thrown during render. So the error thrown in Buggy is caught by the Error Boundary, which renders the fallback UI 'Something went wrong.'.
📝 Syntax
intermediate2:00remaining
Which code correctly defines an Error Boundary in React 19+?
Select the option that correctly implements an Error Boundary component in React 19+ using class components.
Attempts:
2 left
💡 Hint
Error Boundaries require static getDerivedStateFromError or componentDidCatch lifecycle methods.
✗ Incorrect
Option D correctly uses static getDerivedStateFromError to update state on error and renders fallback UI. Option D correctly calls super(props) in constructor (fixed here) and uses componentDidCatch to update state. Options B and C are functional components which cannot catch errors during render.
🔧 Debug
advanced2:00remaining
Why does this Error Boundary not catch errors from a child component?
Given this Error Boundary code, why does it fail to catch errors thrown by its child components?
class MyErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { error: null };
}
componentDidCatch(error) {
console.log('Error caught:', error);
}
render() {
if (this.state.error) return
Oops!
; return this.props.children; } }Attempts:
2 left
💡 Hint
componentDidCatch should update state to trigger fallback UI.
✗ Incorrect
componentDidCatch logs the error but does not update state. Without updating state, the render method never shows fallback UI. The error is caught but UI does not change.
❓ state_output
advanced2:00remaining
What is the state value after an error is caught?
In this Error Boundary, what is the value of this.state.hasError after a child component throws an error?
class Boundary extends React.Component {
state = { hasError: false };
static getDerivedStateFromError() {
return { hasError: true };
}
render() {
if (this.state.hasError) return
Error!
;
return this.props.children;
}
}Attempts:
2 left
💡 Hint
getDerivedStateFromError updates state when an error occurs.
✗ Incorrect
getDerivedStateFromError returns { hasError: true } which updates the state. So after an error, this.state.hasError is true.
🧠 Conceptual
expert2:00remaining
Which errors are NOT caught by React Error Boundaries?
React Error Boundaries catch errors in components during rendering, lifecycle methods, and constructors. Which of these errors will NOT be caught by Error Boundaries?
Attempts:
2 left
💡 Hint
Think about where React Error Boundaries work and where they don't.
✗ Incorrect
React Error Boundaries do NOT catch errors inside event handlers. Those errors must be handled manually. They catch errors during rendering, lifecycle methods, and constructors of child components.