-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Make Routes component participate in data loading #9254
Comments
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 |
@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 |
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 away 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 />,
}
]); |
We have an application that uses lazy-loaded descendant routes. I am running into this scenario when attempting to take advantage of the new |
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! |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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.
The text was updated successfully, but these errors were encountered: