How to have multiple layouts? #7296
-
I'm a little confused with the v2 route convention. My goal is to have 3 different layouts in the same app: one for marketing pages (home, faq, contact, pricing), one for auth pages (login, signup, checkout) and one for the user dashboard, which is only accessible after the user is authenticated. Where should I define each layout? This is my
I would like to share the same layout, with a header and a footer between // routes/_mkt.tsx
export default function MktLayout() {
return (
<>
<Header />
<main>
<Outlet />
</main>
<Footer />
</>
);
} While this works, I'm not sure this is the best approach. I doesn't feel right to have the home page of the site under a route I could move that layout to |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
There are a couple of ways to share a layout. As you state above, the Remix way is to create a layout file (prefix with // routes/_mkt.tsx
export default function MktLayout() {
return (
<>
<Header />
<main>
<Outlet />
</main>
<Footer />
</>
);
} Then, all the routes you want to share that layout with must have that layout as part of the filename. So You can also do it the React way by simply importing the shared layout component. Remember, layouts are basically React components, and // layouts/_mkt.tsx
export default function MktLayout({children}) {
return (
<>
<Header />
<main>
{children}
</main>
<Footer />
</>
)
}
// routes/_index.tsx
import MktLayout from '~/layouts/_mkt'
export default function HomePage() {
return (
<MktLayout>
<HomePageContentsHere/>
</MktLayout>
)
} The main drawback to this approach is that the layout is not treated as a route, so it won't be able to have its own A third option is to use You can create a file structure like this: app/routes
├── _mkt+
│ ├── _index.tsx
│ ├── _layout.tsx
│ ├── contact.tsx
│ ├── faq.tsx
│ └── pricing.tsx
├── auth+
│ ├── _layout.tsx
│ ├── checkout.tsx
│ ├── login.tsx
│ └── signup.tsx
└── dashboard.tsx The |
Beta Was this translation helpful? Give feedback.
There are a couple of ways to share a layout.
As you state above, the Remix way is to create a layout file (prefix with
_
if you don't want to include it in the path).Then, all the routes you want to share that layout with must have that layout as part of the filename. So
_mkt.tsx
is the layout, and the home page would be_mkt._index.tsx
. Remix will render the index route in the<Outlet>
of the_mkt
layout.You can also do it the React way by simply importing the shared layout component. Remember, layouts are basically …