Replies: 9 comments 7 replies
-
I second this. Unless I'm missing something, to use the new However, if you think about it, it just undermines code-splitting techniques. Using routes-at-the-root solution would require either reference route components directly, which would disable code-splitting, or lazy loading routes as well as all of their child routes to make it work. For example, it would be simply impossible to make When a component is required to know about not only its direct children, but also about children of its children, it just breaks incapsulation and becomes an unlikely scalable solution. I understand react-router team might push the community towards using Alternatively, what I thought of is that we could just start nesting prefixed |
Beta Was this translation helpful? Give feedback.
-
@MrHus @maxprilutskiy We will likely eventually add some "recommended" code splitting approaches to the docs, but you have various options available in the current setup. Another community member did a great write-up of some options here: https://dev.to/infoxicator/data-routers-code-splitting-23no. As for making Here's the main challenge with getting descendant
So the idea of allowing descendant We do agree that it's currently non-optimal for large apps to have to specify all of their routes up-front, and we have ideas in mind to alleviate that by adding some form of runtime route discovery, but I don't think that discovery will necessarily happen in the react render lifecycle - more likely it will happen through some other API we can tap into prior to rendering. I'm going to leave this open for now but I suspect this will get converted into a Discussion in which we can continue to figure out the right API for the type of route discovery |
Beta Was this translation helpful? Give feedback.
-
So I wrote this on the remix discord a while ago but I'll leave it here so it doesn't get lost. The current solution makes too many assumptions about the application and takes too much control away from users in my opinion. Kind of related to what @maxprilutskiy said, I think a good solution would be being able to declare multiple "sub" routers, then have a "root" router that composes all of them, very similar to what express.Router does, where you are able to declare a router for an entire route tree and apply middlewares and whatnot to it. Why this is good:
Granted, the "root" router wouldn't be able statically determine the entire route tree but that doesn't matter because each "sub-router" would know the routes it cares about and the data they need to fetch in order to be rendered. Here's a very rough usage as I imagine it: // users/routes.tsx
import { UserLayout } from './UserLayout';
import { NewUser } from './NewUser';
import { UserDetails } from './UserDetails';
import { UsersList } from './UsersList';
import { EditUser } from './EditUser';
const usersRouter = createBrowserRouter([
{
path: '/',
element: <UsersLayout />,
children: [
{ index: true, element: <UsersList /> },
{ path: 'new', element: <NewUser /> },
{
path: ':userId/*',
loader: ({ params }) =>
fetch(`/api/users/${params.userId}`).then((res) => res.json()),
children: [
{ index: true, element: <UserDetails /> },
{
path: 'edit',
element: <EditUser />,
},
],
},
],
},
]);
export default usersRouter;
// main.tsx
// lazy-load usersRouter, users-related components are not included in the initial bundle
const usersRouter = () => import('./users/routes.tsx')
const rootRouter = createBrowserRouter([
{
path: '/',
element: <Layout />,
loader: () => fetch(`/api/config`).then((res) => res.json()),
children: [
{ index: true, element: <Home /> },
// `router` should be able to accept lazy-loaded routers
// `router` and `element` are mutually exclusive
{ path: 'users/*', router: usersRouter },
],
},
{
path: '/login'',
element: <Login />,
}
]); |
Beta Was this translation helpful? Give feedback.
-
We have an application that uses lazy-loaded descendant routes. I am running into this scenario when attempting to take advantage of the new |
Beta Was this translation helpful? Give feedback.
-
I'm going to convert this to a discussion so it can go through our new Open Development process. Please upvote the new Proposal if you'd like to see this considered! |
Beta Was this translation helpful? Give feedback.
-
Very much in favor of this feature to support chunking/lazy-loading bundles of components, rather than per-component. Regardless of the feature, it would be great to clean up some of the error messages that happen in violation of the intended data API use. For example:
It's a little unclear in the usage of |
Beta Was this translation helpful? Give feedback.
-
Please note that errors also don't bubble up with React Router 6.4+ if the If you click the However, if you click the |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
👋 Hey folks, we just published a new Route Discovery RFC that I think should address the concerns in this proposal - please let us know over there if you have comments! |
Beta Was this translation helpful? Give feedback.
-
What is the new or updated feature that you are suggesting?
Make the
<Routes>
component participate in data loading.Why should this feature be included?
Currently I'm using the
<Routes />
component as a way to split up my routes in multiple different files.This way my routes do not grow very large, and I keep things manageable.
Currently I'm looking at the new "loader" feature and how to integrate it in my application. But the loaders
do not work for nested
<Routes />
. The documentation says:"If you're using a data router like createBrowserRouter it is uncommon to use this component as it does not participate in data loading."
It would be nice to support
<Routes />
in combination with the "createBrowserRouter" as to not get a GOD routingfile containing all routes of a large application.
The only way forward I see now using the current API is not using "createRoutesFromElements" and importing
"arrays" and merge them at the top, but this way I loose the "routes as jsx" features.
Beta Was this translation helpful? Give feedback.
All reactions