Open History to see the last commit
1import { ErrorComponent, Link, createFileRoute } from '@tanstack/solid-router'
2import { useQuery } from '@tanstack/solid-query'
3import { postQueryOptions } from '../utils/posts'
4import type { ErrorComponentProps } from '@tanstack/solid-router'
5import { NotFound } from '~/components/NotFound'
6β
7export function PostErrorComponent({ error }: ErrorComponentProps) {
8 return <ErrorComponent error={error} />
9}
10β
11export const Route = createFileRoute('/posts/$postId')({
12 loader: async ({ params: { postId }, context }) => {
13 const data = await context.queryClient.ensureQueryData(
14 postQueryOptions(postId),
15 )
16β
17 return {
18 title: data.title,
19 }
20 },
21 head: ({ loaderData }) => ({
22 meta: loaderData ? [{ title: loaderData.title }] : undefined,
23 }),
24 errorComponent: PostErrorComponent,
25 notFoundComponent: () => {
26 return <NotFound>Post not found</NotFound>
27 },
28 component: PostComponent,
29})
30β
31function PostComponent() {
32 const params = Route.useParams()
33 const postQuery = useQuery(() => postQueryOptions(params().postId))
34β
35 return (
36 <div class="space-y-2">
37 <h4 class="text-xl font-bold underline">{postQuery.data?.title}</h4>
38 <div class="text-sm">{postQuery.data?.body}</div>
39 <Link
40 to="/posts/$postId/deep"
41 params={{
42 postId: postQuery.data!.id,
43 }}
44 activeProps={{ class: 'text-black font-bold' }}
45 class="inline-block py-1 text-blue-800 hover:text-blue-600"
46 >
47 Deep View
48 </Link>
49 </div>
50 )
51}