Error boundaries help catch errors in parts of your app so the whole app doesn't crash. They show a fallback UI instead of a broken screen.
Using error boundaries in React
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { return { hasError: true }; } componentDidCatch(error, errorInfo) { // You can log the error here } render() { if (this.state.hasError) { return <h1>Something went wrong.</h1>; } return this.props.children; } }
Error boundaries must be class components in React.
They catch errors in their child components during rendering, lifecycle methods, and constructors.
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { return { hasError: true }; } render() { if (this.state.hasError) { return <h1>Oops! Something went wrong.</h1>; } return this.props.children; } }
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { return { hasError: true }; } componentDidCatch(error, errorInfo) { console.error('Error caught:', error, errorInfo); } render() { if (this.state.hasError) { return <h2>Sorry, something went wrong.</h2>; } return this.props.children; } }
This app uses an error boundary to catch an error thrown by BuggyComponent. Instead of crashing the whole app, it shows a friendly message.
import React from 'react'; class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { return { hasError: true }; } componentDidCatch(error, errorInfo) { console.log('Error caught:', error.toString()); } render() { if (this.state.hasError) { return <h1>Something went wrong.</h1>; } return this.props.children; } } function BuggyComponent() { throw new Error('I crashed!'); // return <div>This will not render.</div>; } export default function App() { return ( <ErrorBoundary> <BuggyComponent /> </ErrorBoundary> ); }
Error boundaries do not catch errors inside event handlers.
Use multiple error boundaries to isolate errors in different parts of your app.
Always provide a fallback UI so users see something helpful.
Error boundaries catch errors in child components to prevent app crashes.
They must be class components with specific lifecycle methods.
Use them to show fallback UI and improve user experience.
Practice
Solution
Step 1: Understand error boundaries role
Error boundaries catch errors in the rendering phase of child components to prevent the whole app from crashing.Step 2: Identify the correct purpose
They show a fallback UI instead of letting the error crash the app, improving user experience.Final Answer:
To catch JavaScript errors in child components and display a fallback UI -> Option CQuick Check:
Error boundaries catch errors = D [OK]
- Thinking error boundaries manage state
- Confusing error boundaries with performance tools
- Assuming error boundaries handle form validation
Solution
Step 1: Recall error boundary requirements
Error boundaries must be class components that implement static getDerivedStateFromError and componentDidCatch lifecycle methods.Step 2: Match the correct syntax
Only a class component implementing static getDerivedStateFromError and componentDidCatch methods correctly describes this pattern; hooks or functional components cannot be error boundaries.Final Answer:
A class component implementing static getDerivedStateFromError and componentDidCatch methods -> Option AQuick Check:
Error boundaries require class + lifecycle methods = B [OK]
- Trying to use hooks for error boundaries
- Omitting getDerivedStateFromError method
- Using only render method without error handling
BuggyComponent throws an error?class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
return { hasError: true };
}
componentDidCatch(error, info) {
console.log('Error caught:', error);
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
// Usage
<ErrorBoundary>
<BuggyComponent />
</ErrorBoundary>Solution
Step 1: Understand error boundary behavior on error
WhenBuggyComponentthrows an error,getDerivedStateFromErrorsetshasErrorto true.Step 2: Check render output when error state is true
The render method returns <h1>Something went wrong.</h1> instead of the child component UI.Final Answer:
An error messageSomething went wrong.will display -> Option DQuick Check:
Error caught shows fallback UI = A [OK]
- Expecting child UI to render despite error
- Thinking app crashes without error boundary
- Confusing syntax error with runtime error
class MyErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { error: null };
}
componentDidCatch(error, info) {
this.setState({ error });
}
render() {
if (this.state.error) {
return <h2>Error occurred</h2>;
}
return this.props.children;
}
}Solution
Step 1: Check error boundary lifecycle requirements
Error boundaries must implement static getDerivedStateFromError to update state when an error happens.Step 2: Analyze current code
This code updates state inside componentDidCatch, which is not recommended for triggering fallback UI.Final Answer:
Missing static getDerivedStateFromError method to update state on error -> Option AQuick Check:
getDerivedStateFromError required for state update = A [OK]
- Updating state only in componentDidCatch
- Ignoring getDerivedStateFromError method
- Thinking setState is forbidden in componentDidCatch
Solution
Step 1: Understand error boundary scope
Error boundaries catch errors in their child component tree and show fallback UI defined inside them.Step 2: Different fallback UIs require separate boundaries
To show different fallback UIs, create multiple error boundary components each wrapping the target component.Step 3: Evaluate other options
Using one boundary with dynamic props or hooks is not supported; functional components cannot be error boundaries.Final Answer:
Create separate error boundary components each with its own fallback UI and wrap each target component accordingly -> Option BQuick Check:
Different fallback UIs need separate boundaries = C [OK]
- Trying to use one boundary for all with dynamic UI
- Using hooks or functional components as error boundaries
- Wrapping all components in one boundary expecting different UI
Ready to prove it?
Pass the test and this pattern moves unknown
