Is it possible to split routing into multiple files with the new data APIs? #10160
-
In the older Example of the old style: //RouterOne.jsx
const RouterOne = () => {
return (
<Routes>
<Route path='pathOne/*' element={<RouterTwo />} />
</Routes>
)
} //RouterTwo.jsx
const RouterTwo = () => {
return (
<Routes>
<Route path='pathTwo' element={<OtherThing />} />
</Routes>
)
} And |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
I haven't found a way to do this using // src/App.tsx
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import AppRoutes from 'routes';
const router = createBrowserRouter(AppRoutes);
export const App = () => {
return (
<RouterProvider router={router} />
);
}; // src/routes/index.tsx
import AuthRoutes from './auth';
import ProfileRoute from './profile';
const AppRoutes = [
{
path: '/',
element: <Root />
children: [
AuthRoutes,
ProfileRoute,
// etc...
]
];
export default AppRoutes; // src/routes/auth/index.tsx
import LoginRoute from './login';
import LogoutRoute from './logout';
import CallbackRoute from './callback';
const AuthRoutes = {
path: 'auth',
children: [
LoginRoute,
LogoutRoute,
CallbackRoute
]
}
export default AuthRoutes; // src/routes/auth/login/index.tsx
import LoginPage from './LoginPage';
const LoginRoute = {
path: 'login',
element: <LoginPage />
}
export default LoginRoute; // src/routes/profile/index.tsx
import ProfilePage from './ProfilePage';
import loader from './loader';
const ProfileRoute = {
path: 'profile',
element: <ProfilePage />
loader
}
export default ProfileRoute; |
Beta Was this translation helpful? Give feedback.
-
Hello, export const useRoutes = () => {
return (
<>
<Route path='/path' element={<Element />} />
<Route path='/some-other-path' element={<Element />} />
</>
);
}; along with: export const MyRoutes = () => {
const routes = useRoutes();
return (
<Routes>
{routes}
<Route path="*" element={<NotFound />} />
</Routes>
);
}; |
Beta Was this translation helpful? Give feedback.
I haven't found a way to do this using
createRoutesFromElements()
without an error about duplicaterouteId
s. This is how I approached splitting up Routes using module exports.//…