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