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

posts.lazy.tsx

47 lines1.2 KB

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