Custom Layout for Specific Routes in tanstack/router #1102
Replies: 13 comments 30 replies
-
The route structure for layout routes is as follows:
You can see it in action in the Basic File-Based example. |
Beta Was this translation helpful? Give feedback.
-
Is there any way to put the layout file inside a route sub-directory?:
The only working way I found for this case is:
But i find it somehow confusing to have both Maybe it's this bug that makes my first example not working: #1150 ? |
Beta Was this translation helpful? Give feedback.
-
Is it possible to use a layout with route groups as well? So that I could have a layout for the entire group of routes?
|
Beta Was this translation helpful? Give feedback.
-
Plus one for this too. I'm coming from Nextjs where it's common to have a layout file for that dir and it's children. I like the way they implemented as @nilshartmann suggested src/routes
auth/
_layout.tsx // <-- would be nice to have: applied for /auth/ and /auth/sign-up
index.tsx
sign-up.tsx I think this is far clearer than the current alternative of src/routes
auth.tsx // <-- applied for /auth/ and /auth/sign-up
auth/
index.tsx
sign-up.tsx or worse yet src/routes
auth/_layout.tsx
auth/_layout/sign-up.tsx
index.tsx The last one especially is weird imo. |
Beta Was this translation helpful? Give feedback.
-
so confusing... |
Beta Was this translation helpful? Give feedback.
-
I got very confused by this as well, but in a different way. I think the docs should be clear how to use folders with pathless routes. I actually thought it worked like this based on the docs:
This is very counter intuitive based on how node JS imports work.
|
Beta Was this translation helpful? Give feedback.
-
I am migrate from nextjs, after read this discussion, it seems @tanstack/router's layout.tsx should be route.tsx, so the layout of your project should be:
compare to nextjs's app route:
|
Beta Was this translation helpful? Give feedback.
-
I second the NextJs way but once I figured out a decent way to implement layouts it's not too bad. We just need more examples. Check out my solution for this.
Inside of |
Beta Was this translation helpful? Give feedback.
-
I found a workaround: In vite.config.ts:
Then I can define my routes as following: /about |
Beta Was this translation helpful? Give feedback.
-
Thanks for this thread! It was exactly what I need to move forward with Tanstack :) |
Beta Was this translation helpful? Give feedback.
-
Anybody got any luck with also naming the layout files to be lazy? So you can load the routes with layout.lazy.tsx? |
Beta Was this translation helpful? Give feedback.
-
Maybe this helps someone, got it working and shared some details here #1457 (comment) |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Hello tanstack/router community,
I'm working on a React project where I'm using the File-Based routing feature of tanstack/router. I have a specific requirement for which I'm seeking some advice or guidance.
My project structure includes an
index.tsx
file, which I understand represents the root (/
) route. The layout for this is determined by__root.tsx
. However, I'm looking to implement a custom layout for theindex.tsx
page and other related pages, specifically for authentication-related routes like sign-in and sign-up.Here's how I envision the structure:
_auth
_auth.tsx
<-- This should be the Layout for the routes inside_auth
_auth.index.tsx
<-- Represents the/
route (sign-in page)_auth.sign-up.tsx
<-- Represents the/sign-up
route (sign-up page)I want both the sign-in (
/
) and sign-up (/sign-up
) pages to share the same layout defined in_auth.tsx
. Could you please provide some insights or examples on how to achieve this in tanstack/router? Is there a specific way to define that_auth.tsx
should be the layout for both these routes?Any help or pointers towards relevant documentation would be greatly appreciated!
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions