📖 Scenario: You are building a Next.js app that shows a list of products. Sometimes, the data fetching might fail or the page might encounter an error. You want to create a simple error page to show a friendly message when something goes wrong.
🎯 Goal: Create an error.tsx file in the app directory that displays a user-friendly error message. This error page should catch errors and show a heading and a paragraph explaining that something went wrong.
📋 What You'll Learn
Create an error.tsx file exporting a React component named Error.
The Error component should accept a prop called error of type Error.
Display a heading with the text Something went wrong!.
Display the error message inside a paragraph.
Export the Error component as default.
💡 Why This Matters
🌍 Real World
Custom error pages improve user experience by showing friendly messages when something goes wrong in your web app.
💼 Career
Knowing how to handle errors gracefully is important for frontend and full-stack developers working with Next.js or React frameworks.
Progress0 / 4 steps
1
Create the error component file
Create a file named error.tsx inside the app directory. Inside it, write a React functional component named Error that accepts a prop called error of type Error.
NextJS
Hint
Start by defining an interface for the props with error: Error. Then create a function named Error that takes these props.
2
Add a heading to the error component
Inside the Error component, add a <h1> element with the text Something went wrong! to inform users that an error occurred.
NextJS
Hint
Replace the null return with a JSX <h1> element containing the exact text.
3
Display the error message
Below the <h1>, add a <p> element that displays the error message using {error.message}.
NextJS
Hint
Use curly braces inside JSX to insert the error.message value inside a paragraph.
4
Export the error component as default
Make sure the Error component is exported as the default export from error.tsx so Next.js can use it automatically.
NextJS
Hint
The component should already be exported as default. Just verify the export statement is present.
Practice
(1/5)
1. What is the main purpose of the error.tsx file in a Next.js route?
easy
A. To catch and display errors that happen in that route
B. To define the page layout and styles
C. To fetch data from an API
D. To handle user authentication
Solution
Step 1: Understand the role of error.tsx
This file is designed to catch errors that occur in its route and show a friendly message to users.
Step 2: Compare with other options
Options A, C, and D describe unrelated tasks like layout, data fetching, or authentication, which are not the purpose of error.tsx.
Final Answer:
To catch and display errors that happen in that route -> Option A
Quick Check:
Error handling = To catch and display errors that happen in that route [OK]
Hint: Remember: error.tsx shows errors in its route [OK]
Common Mistakes:
Confusing error.tsx with layout.tsx
Thinking error.tsx fetches data
Assuming error.tsx manages authentication
2. Which of the following is the correct way to export an error component in error.tsx for Next.js?
easy
A. const Error = () => {
Error occurred
}; export default Error;
B. function Error() { return
Error occurred
; }
C. export default function Error() { return
Error occurred
; }
D. export function Error() { return
Error occurred
; }
Solution
Step 1: Check correct export syntax
Next.js expects a default export for the error component in error.tsx. export default function Error() { return
Error occurred
; } uses export default function, which is correct.
Step 2: Identify syntax errors in other options
function Error() { return
Error occurred
; } lacks export, so it won't be recognized. export function Error() { return
Error occurred
; } exports a named function, not default. const Error = () => {
Error occurred
}; export default Error; misses a return statement inside the arrow function, causing no output.
Final Answer:
export default function Error() { return <p>Error occurred</p>; } -> Option C
Quick Check:
Default export function = export default function Error() { return
Error occurred
; } [OK]
Hint: Use default export function for error component [OK]
Common Mistakes:
Forgetting to export default
Using named exports instead of default
Missing return statement in arrow function
3. Given this error.tsx component, what will be rendered when an error occurs?
D. A section with a heading and a button that reloads the page
Solution
Step 1: Analyze the JSX structure
The component returns a <section> with role="alert", containing a heading and a button with an onClick that reloads the page.
Step 2: Understand the rendered output
When an error happens, this UI shows a message and a clickable button that refreshes the page, matching A section with a heading and a button that reloads the page.
Final Answer:
A section with a heading and a button that reloads the page -> Option D
Quick Check:
JSX returns alert section with reload button = A section with a heading and a button that reloads the page [OK]
Hint: Look for JSX elements and event handlers to predict output [OK]
Common Mistakes:
Ignoring the button's onClick behavior
Assuming no UI renders on error
Confusing role attribute with content
4. You wrote this error.tsx but it does not show the error UI when an error happens:
function Error() {
return <p>Error!</p>;
}
export default Error;
What is the likely problem?
medium
A. The component name must be lowercase
B. The file must be named error.tsx and placed in the route folder
C. The component is not exported as default
D. The component must return a div, not a p
Solution
Step 1: Check export and component name
The component is exported as default and named with uppercase, which is correct.
Step 2: Verify file placement and naming
Next.js requires the error.tsx file to be in the route folder to catch errors there. If misplaced or misnamed, it won't work.
Final Answer:
The file must be named <code>error.tsx</code> and placed in the route folder -> Option B
Quick Check:
Correct file name and location = The file must be named error.tsx and placed in the route folder [OK]
Hint: Check file name and folder location for error.tsx [OK]
Common Mistakes:
Using lowercase component name
Returning wrong HTML tags
Not placing error.tsx in the route folder
5. You want to improve accessibility in your error.tsx component. Which change best helps screen reader users?
hard
A. Add role="alert" to the container element
B. Use only <div> elements without roles
C. Remove all buttons to avoid confusion
D. Use inline styles to highlight the error text
Solution
Step 1: Understand accessibility roles
Adding role="alert" tells screen readers to announce the error immediately, improving accessibility.
Step 2: Evaluate other options
Use only <div> elements without roles lacks roles, reducing accessibility. Remove all buttons to avoid confusion removes helpful retry buttons. Use inline styles to highlight the error text styles visually but doesn't help screen readers.
Final Answer:
Add <code>role="alert"</code> to the container element -> Option A
Quick Check:
Use ARIA roles for accessibility = Add role="alert" to the container element [OK]
Hint: Use ARIA roles like role="alert" for error messages [OK]
Common Mistakes:
Ignoring ARIA roles for screen readers
Removing interactive elements unnecessarily
Relying only on visual styles for errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.