-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: errorElement bubbling doesnt work through <Routes> Elements #10021
Comments
It seems to be the same issue as #9568. Using This thing made me crazy until I understand that... They are working on a feature to inject routes dynamically, but for now, you need to specify the errorElement on each Route :( There is no way to disable it. |
We have a note about this: https://reactrouter.com/en/main/components/routes This is the same issue as #9568 and the comments there apply here. |
@timdorr I know but it's more than a "Note" or maybe the note should explain the limitations. Until const Routes = ({ children }) => {
return (
<RRDRoutes>
{React.Children.map(children, (child) => {
// If the Route does not have the errorElement, we inject it.
if (!child?.props?.errorElement) {
return React.cloneElement(child, { errorElement: <ErrorElement /> });
}
return child;
})}
</RRDRoutes>
);
}; It's only injecting the property at the top level of Route. Wrapping a Route does not work for me because |
This really needs to be more prominently featured in the docs. I was aware that there was a difference between the new data routers and the previous |
I think there might be some confusion here that I'd like to attempt to clear up. And as always, docs certainly have room for improvement - we're open to docs PRs that would help clarify things from an application-developer point of view.
We did recently add a section explicitly listing the "Data APIs" that only work when using a
We see this as a feature, not a bug! Releasing In order to migrate incrementally from Current app using function App() {
return (
<BrowserRouter>
<Routes>
<Route ... />
<Route ... />
</Routes>
</BrowserRouter>
);
} New app using const router = createBrowserRouter([{ path: '*', element: <Layout /> }]);
// Layout is just your descendant Routes tree from your current app
function Layout() {
return (
<Routes>
<Route ... />
<Route ... />
</Routes>
);
}
// App is now a RouterProvider instead of a BrowserRouter
function App() {
return <RouterProvider router={router} />
} With those changes, your app should function the same. But now that you have the
This is not the intention and I hope the docs aren't implying this. We 100% want users to be mixing and matching these while they incrementally upgrade their apps. You can absolutely render descendant I hope this is useful and if you have any specific docs sections of concern or improvements, PRs are always welcome, or even a new issue with specifics would be helpful for us to look into. Thanks! |
The migration guide from v5 to v6 I think could be clearer about which APIs are inaccessible w/o lifting the routes up to the main route config - it reads as a totally optional step atm, with a slight aside about "future data routing apis" I think it might be helpful to have a page that lists what is incompatible / won't behave the way you might expect && to link to that from the migration guide - like this caveat about Routes |
Hi Matt! Really do appreciate the time you take to work through all these issues 🙂 I understand encouraging mixing and matching old and new style routers and I definitely favor an incremental upgrade path, however, I find the current approach can easily lead to problems:
The mixing and matching is fine I suppose, but I think some sort of guard rail would be a welcome addition as right now this can create unnecessary friction, especially in big(ger) teams. This would at least already create some awareness about misuse of these new features, perhaps pointing to the new docs additions that clarify this a bit further. Personally I also feel like the types should reflect the APIs accordingly, for example through 2 different |
That's fair. FWIW I don't believe there's a simple "make everyone happy" solution here. Originally we were only going to allow JSON route definitions for But, then we'd have gotten a bunch of complaints from the other direction around "I need to change every route to JSON?!?". So We'll see what we can do to make this more explicit in the docs and migration guides for sure though - definitely appreciate the feedback 🙌 |
What version of React Router are you using?
6.8.0
Steps to Reproduce
I built an example: https://codesandbox.io/s/react-router-dom-errorelement-not-bubbling-through-routes-tgcbk0?file=/src/App.js
Take a look at the last Element in App.js:
Expected Behavior
The way it was described in the documentation, I would expect an error to be forwarded to the root even through nested .
Actual Behavior
The default errorElement gets triggered:
The text was updated successfully, but these errors were encountered: