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 an error occurs inside a child component wrapped by an error boundary?
Consider a React app where a parent component uses an error boundary to wrap a child component. If the child component throws an error during rendering, what will be the visible result?
React
function ErrorBoundary({children}) { const [hasError, setHasError] = React.useState(false); React.useEffect(() => { // This is a simplified example, real error boundaries use componentDidCatch }, []); if (hasError) { return <div>Something went wrong.</div>; } return children; } function BuggyComponent() { throw new Error('Crash!'); return <div>All good</div>; } function App() { return <ErrorBoundary><BuggyComponent /></ErrorBoundary>; }
Attempts:
2 left
💡 Hint
Think about what error boundaries are designed to do in React.
✗ Incorrect
Error boundaries catch errors in their child components during rendering and lifecycle methods, then display a fallback UI instead of crashing the whole app.
❓ lifecycle
intermediate1:30remaining
Which lifecycle method is used in class components to catch errors for error boundaries?
In React class components, which lifecycle method allows an error boundary to catch errors from its child components?
Attempts:
2 left
💡 Hint
This method receives error details and is called after an error is thrown.
✗ Incorrect
componentDidCatch(error, info) is the lifecycle method where error boundaries catch errors and can perform side effects like logging.
📝 Syntax
advanced2:30remaining
Identify the correct way to define an error boundary using a class component.
Which of the following class components correctly implements an error boundary in React?
Attempts:
2 left
💡 Hint
Remember that getDerivedStateFromError is static and returns new state, and state should not be mutated directly.
✗ Incorrect
Option A correctly uses static getDerivedStateFromError to update state and componentDidCatch for side effects. Other options misuse lifecycle methods or mutate state directly.
🔧 Debug
advanced2:00remaining
Why does this functional component error boundary not catch errors?
Look at this React functional component intended as an error boundary. Why does it fail to catch errors from its children?
function ErrorBoundary({children}) {
try {
return children;
} catch (e) {
return ;
}
Error caught
;
}
}
function Buggy() {
throw new Error('Oops');
return Safe
;
}
function App() {
return Attempts:
2 left
💡 Hint
Think about how React handles errors during rendering and the limitations of try/catch in JSX.
✗ Incorrect
React only supports error boundaries as class components with lifecycle methods. Functional components cannot catch render errors with try/catch because rendering happens outside the function call.
🧠 Conceptual
expert2:30remaining
What is the main limitation of React error boundaries regarding error types they can catch?
Error boundaries in React catch errors in their child components. Which of the following best describes a limitation of error boundaries?
Attempts:
2 left
💡 Hint
Consider where React error boundaries are designed to catch errors and where they do not.
✗ Incorrect
React error boundaries catch errors during rendering, lifecycle methods, and constructors of child components but do not catch errors in event handlers or asynchronous code.