Skip to content

[Bug]: Errors in nested <Route> components are not bubbling up to parent routes #9568

Closed
@evoyy

Description

@evoyy

What version of React Router are you using?

6.4.0

Steps to Reproduce

An error boundary is specified for the top level data API route, and an error is thrown further down the component tree.

function Root() {
  const router = createBrowserRouter([{
    path: "settings/*",
    element: <Settings />,
    errorElement: <div>Error!</div>,
  }]);

  return (
    <RouterProvider router={router} />
  );
}

function Settings() {
  return (
    <Routes>
      <Route path="email-address" element={<EmailAddress />} />
      <Route path="email-preferences" element={<EmailPreferences />} />
      // ...
    </Routes>
  );
}

function EmailAddress() {
  throw 'Some error';
}

React Router will catch the error and display an error template. The error does not bubble up to the root error boundary.

The only way that errors reach the root error boundary is if an errorElement is specified for every <Route> in order to catch and re-throw the error:

// catch and re-throw
function ErrorBoundary() {
  let error = useRouteError();
  throw error;
};

function Settings() {
  return (
    <Routes>
      <Route path="email-address" element={<EmailAddress />} errorElement={<ErrorBoundary />} />
      <Route path="email-preferences" element={<EmailPreferences />} errorElement={<ErrorBoundary />} />
      // ...
    </Routes>
  );
}

Expected Behavior

I expect that the error will bubble up the root error boundary if no errorBoundary is specified.

According to the documentation:

When a route does not have an errorElement, errors will bubble up through parent routes. This lets you get as granular or general as you like.

Put an errorElement at the top of your route tree and handle nearly every error in your app in one place. Or, put them on all of your routes and allow the parts of the app that don't have errors to continue to render normally.

Actual Behavior

React Router catches the error. It does not bubble up to the root error boundary unless it is re-thrown.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions