0
0
Reactframework~20 mins

Using error boundaries in React - Practice Problems & Coding Challenges

Choose your learning style9 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.