0
0
NextJSframework~10 mins

Error.tsx for route errors in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error.tsx for route errors
Route Error Occurs
Next.js Detects Error
Render Error.tsx Component
Display Error Message to User
User Sees Friendly Error Page
When a route error happens, Next.js shows the Error.tsx component to display a friendly error page.
Execution Sample
NextJS
'use client';
import { useEffect } from 'react';

export default function Error({ error, reset }) {
  useEffect(() => {
    console.error(error);
  }, [error]);

  return (
    <main role="alert" aria-live="assertive">
      <h1>Oops! Something went wrong.</h1>
      <p>{error?.message || 'Unknown error occurred.'}</p>
      <button onClick={() => reset()}>Try Again</button>
    </main>
  );
}
This Error.tsx component shows an error message and a retry button when a route error happens.
Execution Table
StepTriggerState BeforeActionState AfterWhat Re-rendersDOM Change
1Route error detectedNo error shownNext.js renders Error.tsx with error properror object presentError componentError message and button appear
2Component mountserror object presentuseEffect logs error to consoleerror loggedNo re-renderNo DOM change
3User clicks 'Try Again'Error shownreset() called to retry routeerror cleared or retriedApp rerenders routeError page removed, route content shown
4No new errorRoute content shownNormal route renderingNo errorRoute componentRoute page visible
5No further errorsNormal stateNo actionStableNo re-renderNo DOM change
💡 Execution stops when the route successfully loads without errors or user navigates away.
Variable Tracker
VariableStartAfter 1After 2After 3Final
errorundefined{message: 'Failed to load'}{message: 'Failed to load'}undefined or nullundefined or null
resetfunctionfunctioncalledfunctionfunction
Key Moments - 3 Insights
Why does the error message stay visible until I click 'Try Again'?
Because the error prop remains set in Error.tsx (see execution_table step 1 and 3). The component only clears the error when reset() is called.
What does the useEffect hook do in this Error.tsx?
It logs the error to the console once when the component mounts with the error (see execution_table step 2). It does not cause re-render or DOM changes.
How does the reset function help recover from the error?
Calling reset() triggers Next.js to retry loading the route, clearing the error and re-rendering the normal page (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of 'error' at step 2?
AUndefined
BAn error object with a message
CNull
DA string error message
💡 Hint
Check the 'State After' column for step 1 and 'State Before' for step 2 in execution_table.
At which step does the user interaction to retry happen?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the step mentioning 'User clicks Try Again' in the execution_table.
If the reset function was not called, what would happen to the error display?
AError message disappears automatically
BPage reloads automatically
CError message stays visible
DError message changes to a success message
💡 Hint
Refer to the key_moments about error visibility and reset function.
Concept Snapshot
Error.tsx in Next.js handles route errors by rendering a friendly error page.
It receives an error object and a reset function as props.
The component shows the error message and a retry button.
useEffect logs the error once on mount.
Clicking retry calls reset() to reload the route.
This pattern improves user experience on route failures.
Full Transcript
When a route error happens in Next.js, the framework renders the Error.tsx component. This component receives the error details and a reset function. It shows a clear error message and a button labeled 'Try Again'. When the component mounts, it logs the error to the browser console for debugging. The error message stays visible until the user clicks the retry button, which calls the reset function. This triggers Next.js to attempt loading the route again, clearing the error and showing the normal page if successful. This flow helps users understand and recover from route errors smoothly.