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
Create an Error.tsx Component for Route Errors in Next.js
📖 Scenario: You are building a Next.js app that needs a friendly error page for route errors like 404 or 500.This page should show a clear message and a button to go back home.
🎯 Goal: Build an Error.tsx component that displays the error statusCode and a message. Include a button that navigates back to the homepage.
📋 What You'll Learn
Create a functional React component named Error in Error.tsx.
Accept a prop called statusCode of type number.
Display the statusCode and a friendly error message.
Add a button that uses Next.js useRouter to navigate back to the homepage when clicked.
Use semantic HTML and accessible attributes.
💡 Why This Matters
🌍 Real World
Error pages improve user experience by clearly showing when something goes wrong and guiding users back to safety.
💼 Career
Creating accessible and user-friendly error components is a common task for frontend developers working with Next.js or React.
Progress0 / 4 steps
1
Setup the Error component with props
Create a functional React component called Error in Error.tsx. It should accept a prop named statusCode of type number. Export this component as default.
NextJS
Hint
Define an interface ErrorProps with statusCode as a number. Then create a function Error that takes { statusCode } as props and export it.
2
Add basic error message display
Inside the Error component, return a main element containing an h1 that shows Error {statusCode} and a p with the text Sorry, something went wrong.
NextJS
Hint
Use JSX to return a main element with an h1 showing the error code and a p with the message.
3
Import useRouter and add navigation button
Import useRouter from next/router. Inside the Error component, call const router = useRouter(). Add a button with aria-label="Go back to homepage" that calls router.push('/') when clicked.
NextJS
Hint
Import useRouter from next/router. Use it to navigate home when the button is clicked.
4
Add semantic and accessible structure
Wrap the content inside a section with role="alert" for accessibility. Add a footer with a small text Contact support if the problem persists.
NextJS
Hint
Use a section with role="alert" to wrap the error message and button. Add a footer with a small support message below.
Practice
(1/5)
1. What is the main purpose of the Error.tsx file in a Next.js route?
easy
A. To define the main layout of the application
B. To display a friendly message when a route fails or an error occurs
C. To handle user authentication and login
D. To fetch data from an API for the 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 B
Quick Check:
Error.tsx shows friendly error messages [OK]
Hint: Error.tsx shows errors, not layouts or data [OK]
Common Mistakes:
Confusing Error.tsx with layout or data files
Thinking Error.tsx handles authentication
Assuming Error.tsx fetches API data
2. Which of the following is the correct way to export a default Error component in Error.tsx for Next.js?
easy
A. export function Error() { return
Error occurred
; }
B. function Error() { return
Error occurred
; }
C. export default function Error() { return
Error occurred
; }
D. default export function Error() { return
Error occurred
; }
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() { return
Error occurred; } misses export, C exports a named function, D has invalid syntax.
Final Answer:
export default function Error() { return <div>Error occurred</div>; } -> Option C