0
0
NextJSframework~20 mins

Client-side error boundaries in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Client-side 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 client-side error boundary in Next.js?

Consider a client-side error boundary component in Next.js wrapping a child component that throws an error during rendering. What will the user see?

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

  React.useEffect(() => {
    const handleError = () => setHasError(true);
    window.addEventListener('error', handleError);
    return () => window.removeEventListener('error', handleError);
  }, []);

  if (hasError) {
    return <div role="alert">Something went wrong.</div>;
  }

  return children;
}

function BuggyComponent() {
  throw new Error('Crash!');
}

export default function Page() {
  return (
    <ErrorBoundary>
      <BuggyComponent />
    </ErrorBoundary>
  );
}
AThe page crashes and shows a blank screen with a console error.
BThe error is ignored and the buggy component renders nothing silently.
CThe error boundary catches the error and displays 'Something went wrong.' message.
DNext.js automatically reloads the page to recover from the error.
Attempts:
2 left
💡 Hint

Think about what an error boundary is designed to do in React and Next.js.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly defines a client-side error boundary in Next.js?

Choose the code that correctly implements a client-side error boundary using React hooks in Next.js.

A
function ErrorBoundary({ children }) {
  const [hasError, setHasError] = React.useState(false);
  React.useEffect(() =&gt; {
    const handleError = () =&gt; setHasError(true);
    window.addEventListener('error', handleError);
    return () =&gt; window.removeEventListener('error', handleError);
  }, []);
  if (hasError) {
    return &lt;div&gt;Error occurred&lt;/div&gt;;
  }
  return children;
}
B
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;
  }
}
C
function ErrorBoundary({ children }) {
  const [hasError, setHasError] = React.useState(false);
  React.useEffect(() =&gt; {
    try {
      children;
    } catch {
      setHasError(true);
    }
  }, []);
  if (hasError) {
    return &lt;div&gt;Error occurred&lt;/div&gt;;
  }
  return children;
}
D
function ErrorBoundary({ children }) {
  const [error, setError] = React.useState(null);
  if (error) {
    return &lt;div&gt;Error occurred&lt;/div&gt;;
  }
  return children;
}
Attempts:
2 left
💡 Hint

Remember that React error boundaries cannot catch errors inside event handlers or async code, so a window error listener is sometimes used.

🔧 Debug
advanced
2:00remaining
Why does this client-side error boundary fail to catch errors thrown in event handlers?

Review the code below. Why does the error boundary not catch errors thrown inside the button's onClick handler?

NextJS
function ErrorBoundary({ children }) {
  const [hasError, setHasError] = React.useState(false);
  React.useEffect(() => {
    const handleError = () => setHasError(true);
    window.addEventListener('error', handleError);
    return () => window.removeEventListener('error', handleError);
  }, []);
  if (hasError) {
    return <div>Something went wrong.</div>;
  }
  return children;
}

export default function Page() {
  return (
    <ErrorBoundary>
      <button onClick={() => { throw new Error('Click error'); }}>
        Click me
      </button>
    </ErrorBoundary>
  );
}
ABecause errors in event handlers are not caught by React error boundaries or window error events by default.
BBecause the error boundary is missing a try-catch block around the button's onClick handler.
CBecause the window error event listener is not properly removed in the cleanup function.
DBecause the button component is outside the error boundary's children.
Attempts:
2 left
💡 Hint

Think about how React handles errors in event handlers differently from render errors.

state_output
advanced
2:00remaining
What is the value of hasError state after an error is caught by this client-side error boundary?

Given the code below, what will be the value of hasError after an error event is triggered?

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

  React.useEffect(() => {
    const handleError = () => setHasError(true);
    window.addEventListener('error', handleError);
    return () => window.removeEventListener('error', handleError);
  }, []);

  return hasError ? <div>Error occurred</div> : children;
}

// Somewhere else in the app, an error is thrown causing a window error event.
Aundefined
Bfalse
Cnull
Dtrue
Attempts:
2 left
💡 Hint

Consider what the event listener does when an error occurs.

🧠 Conceptual
expert
2:00remaining
Why are client-side error boundaries important in Next.js applications?

Choose the best explanation for why client-side error boundaries are used in Next.js apps.

AThey automatically fix bugs in the code by retrying failed components without user intervention.
BThey prevent the entire React component tree from unmounting when a child component throws an error during rendering, improving user experience by showing fallback UI instead of a blank page.
CThey enable server-side rendering to catch errors before sending HTML to the client.
DThey replace the need for try-catch blocks in all asynchronous code throughout the app.
Attempts:
2 left
💡 Hint

Think about what happens when a React component crashes and how error boundaries help.