Skip to content

Commit 191a1c8

Browse files
docs(router): walkthrough on using the ignore prefix for component colocation in the routing-concepts guide (TanStack#4075)
Co-authored-by: Sean Cassiere <33615041+SeanCassiere@users.noreply.github.com>
1 parent 5e08ac5 commit 191a1c8

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

docs/router/framework/react/routing/routing-concepts.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,50 @@ The following table shows which component will be rendered based on the URL:
318318
- The `posts.$postId.tsx` route is nested as normal under the `posts.tsx` route and will render `<Posts><Post>`.
319319
- The `posts_.$postId.edit.tsx` route **does not share** the same `posts` prefix as the other routes and therefore will be treated as if it is a top-level route and will render `<PostEditor>`.
320320

321+
## Excluding Files and Folders from Routes
322+
323+
Files and folders can be excluded from route generation with a `-` prefix attached to the file name. This gives you the ability to colocate logic in the route directories.
324+
325+
Consider the following route tree:
326+
327+
```
328+
routes/
329+
├── posts.tsx
330+
├── -posts-table.tsx // 👈🏼 ignored
331+
├── -components/ // 👈🏼 ignored
332+
│ ├── header.tsx // 👈🏼 ignored
333+
│ ├── footer.tsx // 👈🏼 ignored
334+
│ ├── ...
335+
```
336+
337+
We can import from the excluded files into our posts route
338+
339+
```tsx
340+
import { createFileRoute } from '@tanstack/react-router'
341+
import { PostsTable } from './-posts-table'
342+
import { PostsHeader } from './-components/header'
343+
import { PostsFooter } from './-components/footer'
344+
345+
export const Route = createFileRoute('/posts')({
346+
loader: () => fetchPosts(),
347+
component: PostComponent,
348+
})
349+
350+
function PostComponent() {
351+
const posts = Route.useLoaderData()
352+
353+
return (
354+
<div>
355+
<PostsHeader />
356+
<PostsTable posts={posts} />
357+
<PostsFooter />
358+
</div>
359+
)
360+
}
361+
```
362+
363+
The excluded files will not be added to `routeTree.gen.ts`.
364+
321365
## Pathless Route Group Directories
322366

323367
Pathless route group directories use `()` as a way to group routes files together regardless of their path. They are purely organizational and do not affect the route tree or component tree in any way.

0 commit comments

Comments
 (0)