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

posts_.$postId.deep.tsx

36 lines1.1 KB

1import { Link, createFileRoute } from '@tanstack/solid-router'
2import { useQuery } from '@tanstack/solid-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 params = Route.useParams()
25 const postQuery = useQuery(() => postQueryOptions(params().postId))
26​
27 return (
28 <div class="p-2 space-y-2">
29 <Link to="/posts" class="block py-1 text-blue-800 hover:text-blue-600">
30 ← All Posts
31 </Link>
32 <h4 class="text-xl font-bold underline">{postQuery.data?.title}</h4>
33 <div class="text-sm">{postQuery.data?.body}</div>
34 </div>
35 )