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

posts_.$postId.deep.tsx

39 lines1.1 KB

1import { Link, createFileRoute } from '@tanstack/react-router'
2import { useSuspenseQuery } from '@tanstack/react-query'
3import { postQueryOptions } from '../utils/posts'
4import { PostErrorComponent } from './posts.$postId'
5​
6export const Route = createFileRoute('/posts_/$postId/deep')({
7 loader: async ({ params: { postId }, context }) => {
8 const data = await context.queryClient.ensureQueryData(
9 postQueryOptions(postId),
10 )
11​
12 return {
13 title: data.title,
14 }
15 },
16 head: ({ loaderData }) => ({
17 meta: loaderData ? [{ title: loaderData.title }] : undefined,
18 }),
19 errorComponent: PostErrorComponent,
20 component: PostDeepComponent,
21})
22​
23function PostDeepComponent() {
24 const { postId } = Route.useParams()
25 const postQuery = useSuspenseQuery(postQueryOptions(postId))
26​
27 return (
28 <div className="p-2 space-y-2">
29 <Link
30 to="/posts"
31 className="block py-1 text-blue-800 hover:text-blue-600"
32 >
33 ← All Posts
34 </Link>
35 <h4 className="text-xl font-bold underline">{postQuery.data.title}</h4>
36 <div className="text-sm">{postQuery.data.body}</div>
37 </div>
38 )