I am using ReactRouter v6 and running into a scenario where some pages require shared parent components but some others not, here is an example of my application (URL to component name):
/
:<UserCenter />
/dashboard
:<Dashboard />
/profile
:<Profile />
/login
:<SignIn />
/logout
:<SignOut />
*
:<NotFoundPage />
The former 3 pages (/, /dashboard and /profile) share multiple common parent components like sidebar, footer, title bar, etc. after a successful login, while the rest 3 (/login, /logout and not found) are full screen pages without parent dependents.
I would like to know how I can organize routes to wrap them programmatically under different parent components, and be able to access them at the same time (because I need to enable navigation, for example, auto redirect to /
if a login user is accessing /login
path)? Thanks!
A pseudo example to what I would like to achieve:
const Routing = (
<div>
// Render these 3 pages in routes directly
<Routes>
<Route path='/login' element={<SignIn />}
<Route path='/logout' element={<SignOut />}
<Route path='*' element={<NotFoundPage />}
</Routes>
// Render these 3 pages with parent components
<Header>
<Sidebar>
<Routes>
<Route path='/' element={<UserCenter />}
<Route path='/dashboard' element={<Dashboard />}
<Route path='/profile' element={<Profile />}
</Routes>
</Sidebar>
</Header>
</div>
);
const APP = () => {
return (
<BrowserRouter>
<Routing />
</BrowserRouter>
)
}