0
0
Reactframework~20 mins

Error boundaries concept 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 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>;
}
AThe error is ignored and the buggy component renders normally.
BThe error boundary catches the error and displays 'Something went wrong.' instead of the buggy component.
CThe app crashes completely and shows a blank screen.
DThe error boundary logs the error but still renders the buggy component's output.
Attempts:
2 left
💡 Hint
Think about what error boundaries are designed to do in React.
lifecycle
intermediate
1: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?
AcomponentDidCatch(error, info)
BshouldComponentUpdate(nextProps, nextState)
CcomponentWillUnmount()
DgetDerivedStateFromError(error)
Attempts:
2 left
💡 Hint
This method receives error details and is called after an error is thrown.
📝 Syntax
advanced
2: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?
A
class ErrorBoundary extends React.Component {
  state = { hasError: false };
  static getDerivedStateFromError(error) {
    return { hasError: true };
  }
  componentDidCatch(error, info) {
    console.log(error, info);
  }
  render() {
    if (this.state.hasError) {
      return &lt;h1&gt;Oops!&lt;/h1&gt;;
    }
    return this.props.children;
  }
}
B
class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }
  componentDidCatch(error) {
    this.setState({ hasError: true });
  }
  render() {
    if (this.state.hasError) {
      return &lt;h1&gt;Error!&lt;/h1&gt;;
    }
    return this.props.children;
  }
}
C
class ErrorBoundary extends React.Component {
  state = { hasError: false };
  getDerivedStateFromError(error) {
    this.setState({ hasError: true });
  }
  render() {
    if (this.state.hasError) {
      return &lt;h1&gt;Error caught&lt;/h1&gt;;
    }
    return this.props.children;
  }
}
D
class ErrorBoundary extends React.Component {
  state = { hasError: false };
  componentDidCatch(error, info) {
    this.state.hasError = true;
  }
  render() {
    if (this.state.hasError) {
      return &lt;h1&gt;Error!&lt;/h1&gt;;
    }
    return this.props.children;
  }
}
Attempts:
2 left
💡 Hint
Remember that getDerivedStateFromError is static and returns new state, and state should not be mutated directly.
🔧 Debug
advanced
2: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 ; }
AThe error boundary must use useEffect hook to catch errors in children.
BThe try/catch block is missing a finally clause to handle errors properly.
CReact error boundaries must be class components; try/catch in functional components does not catch render errors.
DThe Buggy component does not throw an error during rendering, so no error is caught.
Attempts:
2 left
💡 Hint
Think about how React handles errors during rendering and the limitations of try/catch in JSX.
🧠 Conceptual
expert
2: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?
AThey catch all JavaScript errors anywhere in the app, including event handlers and asynchronous code.
BThey catch errors only in functional components but not in class components.
CThey only catch errors in server-side rendering but not in client-side rendering.
DThey catch errors during rendering, lifecycle methods, and constructors of child components but not errors in event handlers.
Attempts:
2 left
💡 Hint
Consider where React error boundaries are designed to catch errors and where they do not.