Bird
Raised Fist0
Reactframework~20 mins

Using error boundaries in React - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Error Boundary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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>;
}
AThe Error Boundary catches the error and renders 'Something went wrong.'
BThe app crashes and shows a blank screen.
CThe error is ignored and 'All good' is displayed.
DReact logs the error but still renders the Buggy component output.
Attempts:
2 left
💡 Hint
Error Boundaries catch errors in lifecycle methods and render phase, but only in class components.
📝 Syntax
intermediate
2: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.
A
class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }
  componentDidCatch() {
    this.setState({ hasError: true });
  }
  render() {
    if (this.state.hasError) return &lt;h1&gt;Error occurred&lt;/h1&gt;;
    return this.props.children;
  }
}
B
function ErrorBoundary({children}) {
  try {
    return children;
  } catch {
    return &lt;h1&gt;Error occurred&lt;/h1&gt;;
  }
}
C
const ErrorBoundary = () =&gt; {
  const [error, setError] = React.useState(false);
  if (error) return &lt;h1&gt;Error occurred&lt;/h1&gt;;
  return &lt;&gt;{children}&lt;/&gt;;
};
D
class ErrorBoundary extends React.Component {
  state = { hasError: false };
  static getDerivedStateFromError() { return { hasError: true }; }
  render() {
    if (this.state.hasError) return &lt;h1&gt;Error occurred&lt;/h1&gt;;
    return this.props.children;
  }
}
Attempts:
2 left
💡 Hint
Error Boundaries require static getDerivedStateFromError or componentDidCatch lifecycle methods.
🔧 Debug
advanced
2: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; } }
AThe constructor is missing super(props), causing a crash.
BcomponentDidCatch does not update state, so fallback UI never shows.
CError Boundaries must be functional components to catch errors.
DThe render method returns children before checking for errors.
Attempts:
2 left
💡 Hint
componentDidCatch should update state to trigger fallback UI.
❓ state_output
advanced
2: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; } }
Atrue
Bnull
Cundefined
Dfalse
Attempts:
2 left
💡 Hint
getDerivedStateFromError updates state when an error occurs.
🧠 Conceptual
expert
2: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?
AErrors in lifecycle methods like componentDidMount
BErrors thrown during rendering of child components
CErrors thrown inside event handlers
DErrors thrown in constructors of child components
Attempts:
2 left
💡 Hint
Think about where React Error Boundaries work and where they don't.

Practice

(1/5)
1. What is the main purpose of an error boundary in React?
easy
A. To handle user input validation in forms
B. To manage state updates in functional components
C. To catch JavaScript errors in child components and display a fallback UI
D. To optimize rendering performance by memoizing components

Solution

  1. Step 1: Understand error boundaries role

    Error boundaries catch errors in the rendering phase of child components to prevent the whole app from crashing.
  2. Step 2: Identify the correct purpose

    They show a fallback UI instead of letting the error crash the app, improving user experience.
  3. Final Answer:

    To catch JavaScript errors in child components and display a fallback UI -> Option C
  4. Quick Check:

    Error boundaries catch errors = D [OK]
Hint: Error boundaries catch errors and show fallback UI [OK]
Common Mistakes:
  • Thinking error boundaries manage state
  • Confusing error boundaries with performance tools
  • Assuming error boundaries handle form validation
2. Which of the following is the correct way to define an error boundary component in React?
easy
A. A class component implementing static getDerivedStateFromError and componentDidCatch methods
B. A functional component using useErrorBoundary hook
C. A class component with only render method
D. A functional component with useEffect to catch errors

Solution

  1. Step 1: Recall error boundary requirements

    Error boundaries must be class components that implement static getDerivedStateFromError and componentDidCatch lifecycle methods.
  2. 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.
  3. Final Answer:

    A class component implementing static getDerivedStateFromError and componentDidCatch methods -> Option A
  4. Quick Check:

    Error boundaries require class + lifecycle methods = B [OK]
Hint: Error boundaries must be class components with special lifecycle methods [OK]
Common Mistakes:
  • Trying to use hooks for error boundaries
  • Omitting getDerivedStateFromError method
  • Using only render method without error handling
3. Given this error boundary component, what will be rendered if 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>
medium
A. Nothing will render because of a syntax error
B. The original BuggyComponent UI will render normally
C. The app will crash with an uncaught error
D. An error message Something went wrong. will display

Solution

  1. Step 1: Understand error boundary behavior on error

    When BuggyComponent throws an error, getDerivedStateFromError sets hasError to true.
  2. 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.
  3. Final Answer:

    An error message Something went wrong. will display -> Option D
  4. Quick Check:

    Error caught shows fallback UI = A [OK]
Hint: Error sets state, triggers fallback UI render [OK]
Common Mistakes:
  • Expecting child UI to render despite error
  • Thinking app crashes without error boundary
  • Confusing syntax error with runtime error
4. Identify the error in this error boundary implementation:
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;
  }
}
medium
A. Missing static getDerivedStateFromError method to update state on error
B. Using setState inside componentDidCatch is not allowed
C. The constructor should not initialize state
D. render method must not return JSX

Solution

  1. Step 1: Check error boundary lifecycle requirements

    Error boundaries must implement static getDerivedStateFromError to update state when an error happens.
  2. Step 2: Analyze current code

    This code updates state inside componentDidCatch, which is not recommended for triggering fallback UI.
  3. Final Answer:

    Missing static getDerivedStateFromError method to update state on error -> Option A
  4. Quick Check:

    getDerivedStateFromError required for state update = A [OK]
Hint: Use getDerivedStateFromError to update state on error [OK]
Common Mistakes:
  • Updating state only in componentDidCatch
  • Ignoring getDerivedStateFromError method
  • Thinking setState is forbidden in componentDidCatch
5. You want to use an error boundary to catch errors in multiple components but show different fallback UIs for each. How should you implement this?
hard
A. Use functional components with hooks to catch errors and render fallback UI
B. Create separate error boundary components each with its own fallback UI and wrap each target component accordingly
C. Use one error boundary with a single fallback UI and pass props to customize it dynamically
D. Wrap all components in one error boundary and conditionally render fallback UI inside children

Solution

  1. Step 1: Understand error boundary scope

    Error boundaries catch errors in their child component tree and show fallback UI defined inside them.
  2. Step 2: Different fallback UIs require separate boundaries

    To show different fallback UIs, create multiple error boundary components each wrapping the target component.
  3. Step 3: Evaluate other options

    Using one boundary with dynamic props or hooks is not supported; functional components cannot be error boundaries.
  4. Final Answer:

    Create separate error boundary components each with its own fallback UI and wrap each target component accordingly -> Option B
  5. Quick Check:

    Different fallback UIs need separate boundaries = C [OK]
Hint: Use multiple error boundaries for different fallback UIs [OK]
Common Mistakes:
  • 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard