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

posts.tsx

54 lines1.4 KB

1import { useState } from 'react'
2import { Link, Outlet, createFileRoute } from '@tanstack/react-router'
3​
4import { fetchPosts } from '~/utils/posts'
5​
6export const Route = createFileRoute('/posts')({
7 head: () => ({
8 meta: [
9 {
10 title: 'Posts page',
11 },
12 ],
13 }),
14 loader: async () => fetchPosts(),
15 component: PostsComponent,
16})
17​
18function PostsComponent() {
19 const posts = Route.useLoaderData()
20 const [hydrationCount, setHydrationCount] = useState(0)
21​
22 return (
23 <div className="p-2 flex gap-2">
24 <button
25 data-testid="posts-parent-hydration-counter"
26 onClick={() => setHydrationCount((count) => count + 1)}
27 >
28 Parent hydration count: {hydrationCount}
29 </button>
30 <ul className="list-disc pl-4">
31 {[...posts, { id: 'i-do-not-exist', title: 'Non-existent Post' }].map(
32 (post) => {
33 return (
34 <li key={post.id} className="whitespace-nowrap">
35 <Link
36 to="/posts/$postId"
37 params={{
38 postId: post.id,
39 }}
40 className="block py-1 text-blue-800 hover:text-blue-600"
41 activeProps={{ className: 'text-black font-bold' }}
42 >
43 <div>{post.title.substring(0, 20)}</div>
44 </Link>
45 </li>
46 )
47 },
48 )}
49 </ul>
50 <hr />
51 <Outlet />
52 </div>
53 )