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

posts.tsx

46 lines1.1 KB

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