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

posts.tsx

47 lines1.2 KB

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