0
0
Reactframework~20 mins

Handling runtime errors in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a React component throws an error during rendering?
Consider a React functional component that throws an error inside its body during rendering. What will be the visible result in the browser if no error boundary is used?
React
function BuggyComponent() {
  throw new Error('Oops!');
  return <div>Hello</div>;
}
AThe component renders nothing and the error is silently ignored.
BReact displays the error message on the page automatically.
CReact shows a blank screen and logs the error in the console.
DThe entire React app crashes and unmounts all components.
Attempts:
2 left
💡 Hint
Think about what React does by default when a component throws an error and no error boundary is present.
📝 Syntax
intermediate
2:00remaining
Which code correctly implements an error boundary in React?
Select the option that correctly defines a React error boundary component.
A
function ErrorBoundary({children}) {
  const [hasError, setHasError] = React.useState(false);
  try {
    return children;
  } catch {
    setHasError(true);
    return &lt;div&gt;Error occurred&lt;/div&gt;;
  }
}
B
function ErrorBoundary({children}) {
  try {
    return children;
  } catch {
    return &lt;div&gt;Error occurred&lt;/div&gt;;
  }
}
C
function ErrorBoundary({children}) {
  const [hasError, setHasError] = React.useState(false);
  React.useEffect(() =&gt; {
    window.onerror = () =&gt; setHasError(true);
  }, []);
  if (hasError) return &lt;div&gt;Error occurred&lt;/div&gt;;
  return children;
}
D
class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }
  static getDerivedStateFromError() {
    return { hasError: true };
  }
  render() {
    if (this.state.hasError) {
      return &lt;div&gt;Error occurred&lt;/div&gt;;
    }
    return this.props.children;
  }
}
Attempts:
2 left
💡 Hint
Error boundaries must be class components with specific lifecycle methods.
🔧 Debug
advanced
2:00remaining
Why does this React error boundary fail to catch errors?
Examine the code below. Why does the error boundary not catch errors thrown inside the component?
React
class ErrorBoundary extends React.Component {
  state = { hasError: false };
  componentDidCatch(error, info) {
    this.setState({ hasError: true });
  }
  render() {
    if (this.state.hasError) return <div>Error caught</div>;
    return this.props.children;
  }
}

function Child() {
  throw new Error('Crash');
  return <div>Child</div>;
}

function App() {
  return (
    <ErrorBoundary>
      <Child />
    </ErrorBoundary>
  );
}
AErrorBoundary lacks getDerivedStateFromError, so it cannot update state on error.
BcomponentDidCatch does not update state synchronously, so the error is missed.
CErrorBoundary catches errors only in lifecycle methods, not during rendering of children.
DThe error is thrown outside React's render phase, so ErrorBoundary cannot catch it.
Attempts:
2 left
💡 Hint
Check React docs on error boundaries and required methods to update state on error.
state_output
advanced
2:00remaining
What is the state value after an error is caught in this error boundary?
Given the following error boundary, what will be the value of this.state.hasError after Child throws an error?
React
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 logged');
  }
  render() {
    if (this.state.hasError) return <div>Error caught</div>;
    return this.props.children;
  }
}

function Child() {
  throw new Error('Crash');
  return <div>Child</div>;
}

function App() {
  return (
    <ErrorBoundary>
      <Child />
    </ErrorBoundary>
  );
}
Anull
Btrue
Cundefined
Dfalse
Attempts:
2 left
💡 Hint
getDerivedStateFromError updates state when an error happens during rendering.
🧠 Conceptual
expert
2:00remaining
Which statement about React error boundaries is true?
Select the only correct statement about React error boundaries.
AError boundaries catch errors during rendering, lifecycle methods, and constructors of child components.
BError boundaries can be implemented using React hooks in functional components.
CError boundaries catch errors in event handlers and asynchronous code automatically.
DError boundaries prevent errors from propagating to the browser console.
Attempts:
2 left
💡 Hint
Recall what kinds of errors error boundaries catch and how they are implemented.