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