A React Router route error shows the default error page instead of my boundary
The default error screen appears instead of my own error componentShort 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.
Fix it fast
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/null2Match what you see, then jump to its fix
- 1
The built in error UI shows for every failureNo boundary is attached to the route treeFix - 2
A parent layout's boundary shows for a child's error, or the reverseThe error reaches a different boundary than you expectedFix - 3
A not found case shows a stack trace style message instead of a 404A missing record is thrown as a plain ErrorFix
- 1
What does the error look like?
The default error screen appears instead of my own error componentA loader throws and the whole page is replacedThe 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" src2Fix
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 devConfirmed by reactrouter.com
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=*.tsx2Fix
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 devConfirmed by reactrouter.com
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=*.tsx2Fix
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/missingConfirmed by reactrouter.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix