I'd like to render some of my routes within my public layout, and some other routes within my private layout, is there a clean way to do this?
Example that obviously doesn't work, but I hope explains roughly what I'm looking for:
<Router>
<PublicLayout>
<Switch>
<Route exact path="/" component={HomePage} />
<Route exact path="/about" component={AboutPage} />
</Switch>
</PublicLayout>
<PrivateLayout>
<Switch>
<Route exact path="/profile" component={ProfilePage} />
<Route exact path="/dashboard" component={DashboardPage} />
</Switch>
</PrivateLayout>
</Router>
I'd like the layout to switch for certain routes, how do I do this with the new react router?
Nesting routes no longer works and gives me this error:
You should not use <Route component> and <Route children> in the same route; <Route children> will be ignored
Edit: Having layouts wrap entire groups of routes also means those layouts are only rendered once as long as you stay in the same private/public group of routes. This is a big deal if your layout has to fetch something from your server for example, as that would happen on every page change if you wrap each page with a layout.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…