Global-error.tsx helps catch and show errors that happen anywhere in your Next.js app. It stops the app from crashing and shows a friendly message instead.
Global-error.tsx for root errors in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
'use client'; export default function GlobalError({ error, reset }) { return ( <div> <h1>Something went wrong!</h1> <pre>{error.message}</pre> <button onClick={() => reset()}>Try again</button> </div> ) }
The component receives error and reset props automatically.
The reset function lets users retry after an error.
'use client'; export default function GlobalError({ error, reset }) { return ( <div> <h2>Oops! An error happened.</h2> <p>{error.message}</p> <button onClick={() => reset()}>Reload</button> </div> ) }
'use client'; export default function GlobalError({ error, reset }) { return ( <main style={{ padding: '2rem', fontFamily: 'Arial' }}> <h1>Error!</h1> <p>{error.message}</p> <button onClick={() => reset()}>Try again</button> </main> ) }
This is a complete GlobalError component for Next.js that shows a red error background, the error message, and a button to retry. It uses semantic HTML and accessible button labeling.
'use client'; import React from 'react'; export default function GlobalError({ error, reset }) { return ( <main style={{ fontFamily: 'Arial, sans-serif', padding: '2rem', backgroundColor: '#f8d7da', color: '#721c24' }}> <h1>Oops! Something went wrong.</h1> <pre style={{ whiteSpace: 'pre-wrap' }}>{error.message}</pre> <button style={{ marginTop: '1rem', padding: '0.5rem 1rem', backgroundColor: '#f5c6cb', border: 'none', borderRadius: '4px', cursor: 'pointer' }} onClick={() => reset()} aria-label="Try again button" > Try again </button> </main> ); }
Always include aria-label on buttons for accessibility.
Use semantic HTML tags like <main>, <h1>, and <button> for better structure.
The reset function reloads the part of the app that failed, so users can try again without a full page refresh.
Global-error.tsx catches errors anywhere in your Next.js app and shows a friendly message.
It receives error and reset props to display info and let users retry.
Use semantic HTML and accessibility features for a better user experience.
Practice
Global-error.tsx file in a Next.js app?Solution
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.Step 2: Identify its main function
It shows a user-friendly error message and allows users to retry or reset the error state.Final Answer:
To catch errors anywhere in the app and show a friendly error message -> Option AQuick Check:
Global-error.tsx handles app-wide errors = A [OK]
- Confusing error handling with styling or routing
- Thinking it manages authentication
- Assuming it only catches errors in one component
GlobalError component in Global-error.tsx using React functional components in Next.js?Solution
Step 1: Identify React functional component with props
Next.js uses React functional components with props like{ error, reset }for Global-error.tsx.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.Final Answer:
export default function GlobalError({ error, reset }) { return{error.message}; } -> Option AQuick Check:
Functional component with props and default export = A [OK]
- Using class components instead of functional
- Missing props in function parameters
- Not exporting the component as default
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
);
}Solution
Step 1: Analyze the JSX structure
The component returns a<main role="alert">with a heading, paragraph showingerror.message, and a button with text "Try again".Step 2: Substitute the error message
The error message is "Network failure", so the paragraph will show that exact text.Final Answer:
<main role="alert"><h1>Oops!</h1><p>Network failure</p><button>Try again</button></main> -> Option BQuick Check:
Rendered output matches JSX with error.message = D [OK]
- Ignoring role attribute
- Changing button text
- Using wrong HTML tags or heading levels
Global-error.tsx component code:export default function GlobalError({ error, reset }) {
return (
Error!
{error}
Retry
);
}Solution
Step 1: Check usage of error prop
Theerrorprop is an object; displaying it directly will show [object Object], not the message.Step 2: Correct way to show error message
Useerror.messageto display the actual error text.Final Answer:
Theerrorprop is used directly instead oferror.message-> Option DQuick Check:
Display error.message, not error object = B [OK]
- Displaying error object directly
- Ignoring error.message property
- Assuming reset needs parentheses in JSX
Global-error.tsx component. Which change best helps screen reader users understand the error state?Solution
Step 1: Understand accessibility roles
Therole="alert"attribute tells screen readers to announce the content immediately, which is important for error messages.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.Final Answer:
Addrole="alert"to the main container to announce the error immediately -> Option CQuick Check:
Use role="alert" for error announcements = C [OK]
- Ignoring ARIA roles for error messages
- Relying on color alone for errors
- Removing interactive elements like retry buttons
