Bird
Raised Fist0
NextJSframework~20 mins

Global-error.tsx for root errors in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Next.js Global Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Global-error.tsx component render when an error occurs?

Consider this Next.js Global-error.tsx component used for root error handling. What will it display when an error is caught?

NextJS
import React from 'react';

export default function GlobalError({ error, reset }: { error: Error; reset: () => void }) {
  return (
    <html lang="en">
      <head>
        <title>Error</title>
      </head>
      <body>
        <main role="alert" aria-live="assertive" style={{ padding: '2rem', fontFamily: 'Arial, sans-serif' }}>
          <h1>Something went wrong!</h1>
          <p>{error.message}</p>
          <button onClick={reset} aria-label="Try again">Try again</button>
        </main>
      </body>
    </html>
  );
}
AA page that reloads automatically without showing any message.
BA blank page with no content because the error is not handled properly.
CA page that shows only the error stack trace without any heading or button.
DA page with a heading 'Something went wrong!', the error message, and a 'Try again' button that calls reset.
Attempts:
2 left
💡 Hint

Look for the JSX elements inside the return statement and what props are used.

📝 Syntax
intermediate
2:00remaining
Which option correctly types the props for GlobalError in TypeScript?

Given the GlobalError component, which option correctly types the props error and reset in TypeScript?

NextJS
export default function GlobalError(props) {
  const { error, reset } = props;
  // component body
}
A(props: { error: string; reset: () => void })
B({ error: string; reset: Function })
C({ error, reset }: { error: Error; reset: () => void })
D(error: Error, reset: () => void)
Attempts:
2 left
💡 Hint

Remember the correct way to type destructured props in a function parameter.

state_output
advanced
2:00remaining
What happens when the 'Try again' button is clicked in Global-error.tsx?

In the GlobalError component, what is the effect of clicking the 'Try again' button?

NextJS
export default function GlobalError({ error, reset }: { error: Error; reset: () => void }) {
  return (
    <button onClick={reset} aria-label="Try again">Try again</button>
  );
}
AIt calls the reset function to retry rendering the root component and clear the error state.
BIt reloads the entire page in the browser.
CIt logs the error to the console but does not change the UI.
DIt closes the error message without retrying.
Attempts:
2 left
💡 Hint

Think about what the reset callback is meant to do in error boundaries.

🔧 Debug
advanced
2:00remaining
Why does this Global-error.tsx cause a hydration mismatch in Next.js?

Examine this code snippet. Why might it cause a hydration mismatch error in Next.js?

NextJS
export default function GlobalError({ error, reset }: { error: Error; reset: () => void }) {
  if (!error) {
    return <p>No error found</p>;
  }
  return (
    <html lang="en">
      <head><title>Error</title></head>
      <body>
        <h1>Error occurred</h1>
        <button onClick={reset}>Try again</button>
      </body>
    </html>
  );
}
ABecause the component conditionally returns different root HTML elements causing mismatch between server and client.
BBecause the <html> element is missing the lang attribute.
CBecause the error prop is not typed correctly causing runtime errors.
DBecause the reset function is not called properly on button click.
Attempts:
2 left
💡 Hint

Consider how Next.js expects consistent root elements for hydration.

🧠 Conceptual
expert
2:00remaining
What is the role of Global-error.tsx in Next.js App Router error handling?

In Next.js App Router, what is the primary purpose of the Global-error.tsx file placed at the root of the app directory?

AIt defines global CSS styles for error pages only.
BIt acts as the root error boundary to catch and display errors that happen anywhere in the app tree.
CIt automatically logs errors to an external monitoring service.
DIt replaces the default 404 page with a custom error message.
Attempts:
2 left
💡 Hint

Think about how Next.js handles errors in the new App Router structure.

Practice

(1/5)
1. What is the main purpose of the Global-error.tsx file in a Next.js app?
easy
A. To catch errors anywhere in the app and show a friendly error message
B. To define global CSS styles for the app
C. To handle user authentication and sessions
D. To configure API routes for the backend

Solution

  1. Step 1: Understand the role of Global-error.tsx

    This file is designed to catch errors that happen anywhere in the Next.js app, acting as a global error boundary.
  2. Step 2: Identify its main function

    It shows a user-friendly error message and allows users to retry or reset the error state.
  3. Final Answer:

    To catch errors anywhere in the app and show a friendly error message -> Option A
  4. Quick Check:

    Global-error.tsx handles app-wide errors = A [OK]
Hint: Global-error.tsx handles errors globally, not styles or auth [OK]
Common Mistakes:
  • Confusing error handling with styling or routing
  • Thinking it manages authentication
  • Assuming it only catches errors in one component
2. Which of the following is the correct way to define the GlobalError component in Global-error.tsx using React functional components in Next.js?
easy
A. export default function GlobalError({ error, reset }) { return
{error.message}
; }
B. class GlobalError extends React.Component { render() { return
Error
; } } export default GlobalError;
C. const GlobalError = () => { return
Error
; }; export default GlobalError;
D. function GlobalError() { return
Error
; }

Solution

  1. Step 1: Identify React functional component with props

    Next.js uses React functional components with props like { error, reset } for Global-error.tsx.
  2. Step 2: Check correct export and props usage

    export default function GlobalError({ error, reset }) { return
    {error.message}; } correctly defines a function with props and exports it as default.
  3. Final Answer:

    export default function GlobalError({ error, reset }) { return
    {error.message}
    ; }
    -> Option A
  4. Quick Check:

    Functional component with props and default export = A [OK]
Hint: Use functional component with props and default export [OK]
Common Mistakes:
  • Using class components instead of functional
  • Missing props in function parameters
  • Not exporting the component as default
3. Given this Global-error.tsx snippet, what will be rendered when an error with message "Network failure" occurs?
export default function GlobalError({ error, reset }) {
  return (
    

Oops!

{error.message}

Try again
); }
medium
A.

Oops!

Network failure

B.

Oops!

Network failure

Try again
C.

Oops!

Error occurred

Retry
D.

Error

Network failure

Solution

  1. Step 1: Analyze the JSX structure

    The component returns a <main role="alert"> with a heading, paragraph showing error.message, and a button with text "Try again".
  2. Step 2: Substitute the error message

    The error message is "Network failure", so the paragraph will show that exact text.
  3. Final Answer:

    <main role="alert"><h1>Oops!</h1><p>Network failure</p><button>Try again</button></main> -> Option B
  4. Quick Check:

    Rendered output matches JSX with error.message = D [OK]
Hint: Match JSX tags and error.message exactly [OK]
Common Mistakes:
  • Ignoring role attribute
  • Changing button text
  • Using wrong HTML tags or heading levels
4. Identify the error in this Global-error.tsx component code:
export default function GlobalError({ error, reset }) {
  return (
    

Error!

{error}

Retry
); }
medium
A. The reset function is not called correctly
B. The button text should be 'Try again' instead of 'Retry'
C. The component should use <main> instead of <div>
D. The error prop is used directly instead of error.message

Solution

  1. Step 1: Check usage of error prop

    The error prop is an object; displaying it directly will show [object Object], not the message.
  2. Step 2: Correct way to show error message

    Use error.message to display the actual error text.
  3. Final Answer:

    The error prop is used directly instead of error.message -> Option D
  4. Quick Check:

    Display error.message, not error object = B [OK]
Hint: Always use error.message to show error text [OK]
Common Mistakes:
  • Displaying error object directly
  • Ignoring error.message property
  • Assuming reset needs parentheses in JSX
5. You want to improve accessibility in your Global-error.tsx component. Which change best helps screen reader users understand the error state?
hard
A. Remove the button to avoid confusion
B. Use <div> instead of semantic tags like <main>
C. Add role="alert" to the main container to announce the error immediately
D. Use only color to indicate the error without text

Solution

  1. Step 1: Understand accessibility roles

    The role="alert" attribute tells screen readers to announce the content immediately, which is important for error messages.
  2. Step 2: Evaluate other options

    Using semantic tags is good, but role="alert" is more critical for error announcements. Removing buttons or relying on color alone harms accessibility.
  3. Final Answer:

    Add role="alert" to the main container to announce the error immediately -> Option C
  4. Quick Check:

    Use role="alert" for error announcements = C [OK]
Hint: Use role="alert" to notify screen readers instantly [OK]
Common Mistakes:
  • Ignoring ARIA roles for error messages
  • Relying on color alone for errors
  • Removing interactive elements like retry buttons