What if your app could catch all crashes and still stay friendly to users?
Why Global-error.tsx for root errors in NextJS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine your Next.js app crashes on the homepage, and users just see a blank screen or a confusing error message.
You try to catch errors manually on every page, but it's hard to cover all cases.
Manually handling errors on each page is repetitive and easy to forget.
Users get frustrated because they see ugly error messages or no feedback at all.
Debugging becomes harder without a central place to catch root errors.
Using Global-error.tsx lets you catch all root errors in one place.
This component shows a friendly message and lets users recover gracefully.
It keeps your app stable and improves user experience.
try { renderPage() } catch (e) { showAlert('Error!') }
export default function GlobalError({ error }) { return <div>Oops! Something went wrong.</div> }You can handle all unexpected errors globally, giving users clear feedback and keeping your app reliable.
When a server fails or a bug crashes your app, Global-error.tsx shows a nice error page instead of a blank screen.
Manual error handling is scattered and unreliable.
Global-error.tsx centralizes root error handling in Next.js.
This improves user experience and app stability.
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
