An Error.tsx file helps show a friendly message when something goes wrong on a page. It stops the app from crashing and tells users there was a problem.
Error.tsx for route errors in NextJS
Start learning this pattern below
Jump into concepts and practice - no test required
'use client'; import { useEffect } from 'react'; export default function Error({ error, reset }) { useEffect(() => { console.error(error); }, [error]); return ( <main role="alert" aria-live="assertive" style={{ padding: '2rem', textAlign: 'center' }}> <h1>Oops! Something went wrong.</h1> <p>{error.message}</p> <button onClick={() => reset()}>Try again</button> </main> ); }
The Error component receives an error object and a reset function.
Use aria-live="assertive" and role="alert" for accessibility so screen readers announce the error.
'use client'; export default function Error({ error, reset }) { return ( <div> <h2>Error occurred:</h2> <p>{error.message}</p> <button onClick={reset}>Reload</button> </div> ); }
export default function Error({ error }) { return <p style={{ color: 'red' }}>Error: {error.message}</p>; }
This Error.tsx component catches route errors in Next.js. It logs the error to the console for developers and shows a clear message to users. The button calls reset() to retry loading the route.
'use client'; import { useEffect } from 'react'; export default function Error({ error, reset }) { useEffect(() => { console.error('Route error:', error); }, [error]); return ( <main role="alert" aria-live="assertive" style={{ padding: '2rem', textAlign: 'center', fontFamily: 'Arial, sans-serif' }}> <h1>Oops! Something went wrong.</h1> <p>{error.message}</p> <button onClick={() => reset()} style={{ padding: '0.5rem 1rem', fontSize: '1rem', cursor: 'pointer' }}> Try again </button> </main> ); }
Always include aria-live and role attributes for screen reader users.
The reset function helps users retry loading the page without refreshing the whole browser.
Logging errors in useEffect helps developers find issues during debugging.
Error.tsx shows friendly messages when routes fail.
It improves user experience by preventing blank or broken pages.
Use accessibility features and provide a retry button for best results.
Practice
Error.tsx file in a Next.js route?Solution
Step 1: Understand the role of Error.tsx in Next.js
Error.tsx is designed to catch errors in routes and show a user-friendly message instead of a broken page.Step 2: Compare with other options
Layouts, authentication, and data fetching are handled elsewhere, not in Error.tsx.Final Answer:
To display a friendly message when a route fails or an error occurs -> Option BQuick Check:
Error.tsx shows friendly error messages [OK]
- Confusing Error.tsx with layout or data files
- Thinking Error.tsx handles authentication
- Assuming Error.tsx fetches API data
Error.tsx for Next.js?Solution
Step 1: Recall correct default export syntax in React/Next.js
The correct syntax is to declare the function and export it as default in one statement.Step 2: Identify syntax errors in other options
function Error() { returnError occurred; } misses export, C exports a named function, D has invalid syntax.Final Answer:
export default function Error() { return <div>Error occurred</div>; } -> Option CQuick Check:
Default export uses 'export default function' [OK]
- Forgetting to export the component
- Using named export instead of default
- Incorrect export syntax like 'default export'
Error.tsx component, what will be rendered when an error occurs?export default function Error() {
return (
<main role="alert" aria-live="assertive">
<h1>Oops! Something went wrong.</h1>
<button onClick={() => window.location.reload()}>Try Again</button>
</main>
);
}Solution
Step 1: Analyze the JSX returned by the Error component
The component returns a main element with a heading and a button that reloads the page on click.Step 2: Understand the button behavior
The button triggers window.location.reload(), so it reloads the current page when clicked.Final Answer:
A heading with an error message and a button to reload the page -> Option AQuick Check:
Error component shows message + reload button [OK]
- Assuming no content renders
- Thinking button does nothing
- Expecting error stack trace to show
Error.tsx component code?export default function Error() {
return (
<div>
<h1>Error!</h1>
<button onClick={reloadPage}>Reload</button>
</div>
);
}
function reloadPage() {
location.reload;
}Solution
Step 1: Check the reloadPage function implementation
location.reload is a function and must be called with parentheses: location.reload()Step 2: Verify other parts
The button can have onClick, using div is allowed, and reloadPage can be outside the component.Final Answer:
The reloadPage function does not call location.reload() correctly -> Option AQuick Check:
Call functions with parentheses to execute [OK]
- Forgetting parentheses on function calls
- Thinking onClick is invalid on button
- Believing HTML tags must be <main>
Error.tsx component by adding ARIA roles and live regions. Which of these changes is best practice?Solution
Step 1: Understand ARIA roles for error messages
Using role="alert" and aria-live="assertive" notifies screen readers immediately about errors.Step 2: Evaluate other options
aria-hidden="true" hides content from screen readers, tabindex="-1" on button is unrelated, removing ARIA reduces accessibility.Final Answer:
Wrap the error message in a <main> with role="alert" and aria-live="assertive" -> Option DQuick Check:
Use role="alert" and aria-live for error accessibility [OK]
- Hiding error messages from screen readers
- Misusing tabindex on unrelated elements
- Removing ARIA attributes thinking they are optional
