How do layouts work with the router? #987
Replies: 3 comments 15 replies
-
Hello, answer a bit late, but for a future seekers will write what I found, because documentation lack about layouts mechanics. There are at least 2 options of organising folders to use layouts: Option 1:
Option 2:
So as you see we should intentionally mark which layout we want to use. Inside layouts use |
Beta Was this translation helpful? Give feedback.
-
For anybody else trying to understand how this work, this setup is working for me currently. I can hit the url with the correct layout applied as written in
![]()
// routes/campaigns/route.tsx
import { createFileRoute, Outlet } from "@tanstack/react-router";
export const Route = createFileRoute("/campaigns")({
component: CampaignsLayout,
});
export function CampaignsLayout() {
return (
<div className="flex flex-col gap-4 p-4 pt-0 bg-red-500">
<h1>Campaigns Layout</h1>
<Outlet />
</div>
);
} // routes/campaigns/create.tsx
import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/campaigns/create")({
component: RouteComponent,
});
function RouteComponent() {
return <div>Hello "/campaigns/create"!</div>;
} |
Beta Was this translation helpful? Give feedback.
-
I'm gonna be honest here the file-based layout/page system feels bad big time. Considering the callout to being inspired by Next.js when it comes to the file-based routing, of all things this should've been the thing taken from Next.js:
This would be so incredibly simple, as it is in Next. You get a layout.tsx from /dashboard matching all child routes unless manually escaped, you get your base /dashboard page, and each subsequent page can be added in a folder. Nice, clean, quick and concise. Some people might argue creating a folder for each page.tsx is cumbersome, but in reality it's also nice for nesting page-specific component, style, asset files etc, not just the visualized routing logic. In contrast TanStack Router's handling of Layouts is, if anything frustrating. Doesn't help that nearly every single search result in docs lead you to 404 pages. Click any result when searching "layout" and it's all 404 pages. The only relevant section in "Routing Concepts" has "TODO" as content. As of now, it seems like the play is to have this file structure:
Is it considered valid? Is it the best way? Who knows! Zero documentation, jesus take the wheel! Note that while you can do the route.tsx trick in the above comment for nested pages like /dashboard, it doesn't seem to work for front-facing pages like "/", so it's a good idea to just be consistent and do _layout across the board. And even though this does work, it still feels and looks like a hack. I also don't recommend using route.tsx as a layout hack because the filename vs its purpose creates confusion. Just compare tsrouter vs nextjs in a situation where you want separate layouts for front-facing pages (like "/", "/about-us") and dashboard pages: Is there a cleaner or more accessible tsrouter file-based routing option for setting up layouts? Once again, who knows! Layout documentation is empty. Discussions, reddit threads, youtube comments & co do nothing but complain about tsrouter layout and its lack of documentation. Don't believe me? Google it and click on the first youtube result, read the comments. 11 months ago. I love the TanStack stuff, but using TanStack Router's file-based routing after using Next.js for so long makes it feel hacky, not intuitive. Maybe it's me being nitpicky, but the current setup for layout inheritance both feels and looks bad. Just my two cents. PS: The consistent passive-aggressive "Write the docs yourself" responses from maintainers when it's being brought to your attention that your documentation lacks some of the most foundational pieces of content to your library, which you chose to take responsibility for as a maintainer, also definitely doesn't look good. We're not talking about nice-to-haves here, it's a core feature. Search results not all being 404 links would be a good start. |
Beta Was this translation helpful? Give feedback.
-
Hi, I'd like to start by thanking you for building the router - it is great and super nice.
I am trying to understand how router (v1.4.2) + layouts work. If you can help me learn more, it would be great.
I have the following file-system structure:
The code in
./_layout.tsx
:The code in
./dash/_layout.tsx
:This generates the following routeTree.gen.ts.
Should the layouts in
auth
anddash
directories be configured to have their own paths or should they be treated differently?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions