0
0
NextJSframework~20 mins

Error recovery with reset in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Recovery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the error boundary's reset function is called?

Consider a Next.js error boundary component that uses a reset function to clear the error state. What is the expected behavior after calling reset?

NextJS
import { useState } from 'react';

function ErrorBoundary({ children }) {
  const [hasError, setHasError] = useState(false);

  function reset() {
    setHasError(false);
  }

  if (hasError) {
    return <button onClick={reset}>Try again</button>;
  }

  return children;
}
AThe component unmounts itself and does not show anything.
BThe component throws the error again immediately after reset.
CThe component remains in error state and does not re-render children.
DThe component clears the error and re-renders its children normally.
Attempts:
2 left
💡 Hint

Think about what resetting error state means for rendering.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Next.js error boundary reset handler

Which option correctly fixes the syntax error in this reset handler?

NextJS
function reset() {
  setHasError = false;
}
AChange to <code>setHasError(false);</code>
BChange to <code>setHasError == false;</code>
CChange to <code>setHasError(false) => {}</code>
DChange to <code>setHasError: false;</code>
Attempts:
2 left
💡 Hint

Remember how to call a state setter function in React.

🔧 Debug
advanced
2:00remaining
Why does the error boundary not reset after clicking the reset button?

Given this error boundary code, why does clicking the reset button not clear the error?

NextJS
function ErrorBoundary({ children }) {
  const [hasError, setHasError] = useState(false);

  function reset() {
    hasError = false;
  }

  if (hasError) {
    return <button onClick={reset}>Try again</button>;
  }

  return children;
}
ABecause the reset function is not bound to the button's onClick event.
BBecause directly assigning to hasError does not update React state.
CBecause the component does not render the button when hasError is true.
DBecause useState cannot be used inside error boundaries.
Attempts:
2 left
💡 Hint

Think about how React state updates work.

state_output
advanced
2:00remaining
What is the value of hasError after reset is called twice?

In this error boundary, what is the value of hasError after calling reset() two times in a row?

NextJS
const [hasError, setHasError] = useState(true);

function reset() {
  setHasError(false);
}

reset();
reset();
Aundefined
Btrue
Cfalse
Dnull
Attempts:
2 left
💡 Hint

Consider how React batches state updates.

🧠 Conceptual
expert
2:00remaining
Why is resetting error state important in Next.js error boundaries?

Which reason best explains why implementing a reset function in Next.js error boundaries improves user experience?

AIt allows users to recover from errors without reloading the entire page.
BIt prevents errors from ever occurring in the application.
CIt automatically fixes bugs in the code during runtime.
DIt disables error logging to reduce console noise.
Attempts:
2 left
💡 Hint

Think about what users want when an error happens.