In the following react app, I have a Layout
component which contains a Navbar
and a Footer
(these are the same for all pages and thus they never need to be re-rendered) ...
<BrowserRouter>
<Layout>
<Navbar />
<Footer />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</Layout>
</BrowserRouter>
... and a Content
and a Sidebar
-- these depend on the page, and thus they are rendered from the page component, for example:
const About = () => {
return (
<>
<Content>
...
</Content>
<Sidebar>
...
</Sidebar>
</>
)
}
I want Contact
to use a different layout: same Navbar
and a Footer
but no Sidebar
and different dimensions for content
(i.e. I cannot just not render the sidebar). Is there a way to do this without having to declare <Navbar/>
and <Footer/>
in multiple places?
The only way I see is to use nested layouts, i.e. a parent layout with navbar and footer, and then child layouts either with sidebar and smaller content or without sidebar and bigger content. However, I'd like to avoid nested layouts if possible.