Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

posts.$postId.tsx

34 lines1014 Bytes

1import { Link, createFileRoute } from '@tanstack/react-router'
2import { fetchPost } from '../utils/posts'
3import { NotFound } from '~/components/NotFound'
4import { PostErrorComponent } from '~/components/PostError'
5​
6export const Route = createFileRoute('/posts/$postId')({
7 loader: ({ params: { postId } }) => fetchPost({ data: postId }),
8 errorComponent: PostErrorComponent,
9 component: PostComponent,
10 notFoundComponent: () => {
11 return <NotFound>Post not found</NotFound>
12 },
13})
14​
15function PostComponent() {
16 const post = Route.useLoaderData()
17​
18 return (
19 <div className="space-y-2">
20 <h4 className="text-xl font-bold underline">{post.title}</h4>
21 <div className="text-sm">{post.body}</div>
22 <Link
23 to="/posts/$postId/deep"
24 params={{
25 postId: String(post.id),
26 }}
27 activeProps={{ className: 'text-black font-bold' }}
28 className="inline-block py-1 text-blue-800 hover:text-blue-600"
29 >
30 Deep View
31 </Link>
32 </div>
33 )