Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A React Router route error shows the default error page instead of my boundary

ErrorThe default error screen appears instead of my own error component

Short answer

React Router catches errors from loaders, actions and rendering and shows the closest ErrorBoundary up the route tree. If you see the default page, the route has no boundary of its own, the boundary is attached in the wrong way for your mode, or a nearer route owns the error.

  • Applies to React Router 7, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check how routes are declared. In Data Mode a boundary is the ErrorBoundary property of the route object; the root route should have one.

    grep -rn "ErrorBoundary" src app 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The built in error UI shows for every failureNo boundary is attached to the route treeFix
    2. 2A parent layout's boundary shows for a child's error, or the reverseThe error reaches a different boundary than you expectedFix
    3. 3A not found case shows a stack trace style message instead of a 404A missing record is thrown as a plain ErrorFix

What does the error look like?

  • The default error screen appears instead of my own error component
  • A loader throws and the whole page is replaced
  • The wrong boundary renders for an error in a child route

Cause 1 of 3

No boundary is attached to the route tree

1Check

Apps should export at least a root error boundary. Without one, React Router falls back to its default.

grep -rn "createBrowserRouter" src

2Fix

Add an ErrorBoundary property to the root route, and read the error with useRouteError. Separate thrown responses, error objects and anything else.

import { createBrowserRouter, isRouteErrorResponse, useRouteError } from "react-router";

const router = createBrowserRouter([
  { path: "/", Component: Root, ErrorBoundary: RootErrorBoundary },
]);

function RootErrorBoundary() {
  const error = useRouteError();
  if (isRouteErrorResponse(error)) return <h1>{error.status} {error.statusText}</h1>;
  if (error instanceof Error) return <p>{error.message}</p>;
  return <h1>Unknown Error</h1>;
}

3Verify

Throw an error in a loader on purpose and reload the route.

npm run dev

Confirmed by reactrouter.com

Did this fix it?

Cause 2 of 3

The error reaches a different boundary than you expected

1Check

The closest boundary up the route tree renders. A route without its own boundary hands its errors to its parent.

grep -rn "ErrorBoundary" src --include=*.tsx

2Fix

Add a boundary to the route whose content should stay on screen, so errors in its children render inside it rather than replacing the whole parent.

{ path: "invoices/:id", Component: Invoice, ErrorBoundary: InvoiceErrorBoundary }

3Verify

Trigger an error inside that route and confirm which boundary shows.

npm run dev

Confirmed by reactrouter.com

Did this fix it?

Cause 3 of 3

A missing record is thrown as a plain Error

1Check

Intentional throws are discouraged except for 404s, which should be thrown with a status so the boundary can recognise them.

grep -rn "throw" src --include=*.tsx

2Fix

Throw data with a status 404, then handle it in the isRouteErrorResponse branch.

import { data } from "react-router";

export async function loader({ params }) {
  const record = await getRecord(params.id);
  if (!record) throw data("Record Not Found", { status: 404 });
  return record;
}

3Verify

Open a URL with an id that does not exist.

curl -I http://localhost:5173/invoices/missing

Confirmed by reactrouter.com

Did this fix it?

How do I stop it coming back?

  • Always ship a root error boundary.
  • Do not use boundaries for form validation; return the errors as data instead.
  • Put a boundary on every route that has its own layout, so one failure does not blank the page.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.