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

posts.$postId.tsx

39 lines1.1 KB

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