Open History to see the last commit
1import * as React from 'react'
2import { Link, Outlet, useLoaderData, useMatch } from '@tanstack/react-router'
3import { routeConfig } from '../routes.generated/posts'
4import { PostType } from './posts/$postId'
5import { postspostIdRoute } from '../routes.generated/posts/$postId.client'
6β
7routeConfig.generate({
8 component: Posts,
9 loader: async () => {
10 return {
11 posts: await fetchPosts(),
12 }
13 },
14 errorComponent: () => 'Oh crap',
15})
16β
17async function fetchPosts() {
18 console.log('Fetching posts...')
19 await new Promise((r) => setTimeout(r, 300 + Math.round(Math.random() * 300)))
20 return fetch('https://jsonplaceholder.typicode.com/posts')
21 .then((d) => d.json() as Promise<PostType[]>)
22 .then((d) => d.slice(0, 10))
23}
24β
25function Posts() {
26 const { posts } = useLoaderData({ from: routeConfig.id })
27β
28 return (
29 <div className="p-2 flex gap-2">
30 <ul className="list-disc pl-4">
31 {posts?.map((post) => {
32 return (
33 <li key={post.id} className="whitespace-nowrap">
34 <Link
35 to={postspostIdRoute.id}
36 params={{
37 postId: post.id,
38 }}
39 className="block py-1 text-blue-800 hover:text-blue-600"
40 activeProps={{ className: 'text-black font-bold' }}
41 >
42 <div>{post.title.substring(0, 20)}</div>
43 </Link>
44 </li>
45 )
46 })}
47 </ul>
48 <hr />
49 <Outlet />
50 </div>
51 )
52}