Create a Global-error.tsx for Root Errors in Next.js
📖 Scenario: You are building a Next.js app that needs a friendly error page to show when something goes wrong at the root level. This helps users understand there was a problem without seeing confusing technical details.
🎯 Goal: Build a Global-error.tsx component that catches root errors and displays a simple message with a button to reload the page.
📋 What You'll Learn
Create a React functional component named
GlobalError in Global-error.tsx.Use the
error and reset props to show error details and reset the error state.Display a heading with the text
Something went wrong!.Show the error message inside a
<pre> tag for readability.Add a button labeled
Try again that calls the reset function when clicked.Use semantic HTML and accessible attributes like
aria-label.Style the component with simple inline styles for clarity.
💡 Why This Matters
🌍 Real World
Global error components help apps show friendly messages when something unexpected happens, improving user trust and experience.
💼 Career
Understanding error boundaries and global error handling is important for building robust React and Next.js applications in professional development.
Progress0 / 4 steps