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

posts.lazy.tsx

40 lines1.2 KB

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