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

renderPosts.tsx

63 lines1.6 KB

1'use server'
2​
3import React from 'react'
4import { fetchPosts } from './posts'
5​
6export async function renderPosts() {
7 const posts = await fetchPosts()
8​
9 posts.state = posts.state || {
10 status: 'pending',
11 promise: Promise.resolve(),
12 }
13​
14 return (
15 <div className="p-2 flex gap-2">
16 <ul className="list-disc pl-4">
17 {[...posts, { id: 'i-do-not-exist', title: 'Non-existent Post' }]?.map(
18 (post) => {
19 return (
20 <li key={post.id} className="whitespace-nowrap">
21 {post.title.substring(0, 20)}
22 {/* <Link
23 to="/posts/$postId"
24 params={{
25 postId: post.id,
26 }}
27 className="block py-1 text-blue-800 hover:text-blue-600"
28 activeProps={{ className: 'text-black font-bold' }}
29 >
30 <div>{post.title.substring(0, 20)}</div>
31 </Link> */}
32 </li>
33 )
34 },
35 )}
36 </ul>
37 <hr />
38 <React.Suspense fallback={<div>Loading...</div>}>
39 <DelayedDateViaSuspense state={posts.state} />
40 </React.Suspense>
41 </div>
42 )
44​
45function DelayedDateViaSuspense({ state }) {
46 // a component that will suspend for 1 second and then show the current date
47 if (state.status === 'pending') {
48 state.promise = new Promise<void>((resolve) => {
49 setTimeout(() => {
50 state.status = 'success'
51 resolve()
52 }, 5000)
53 })
54 throw state.promise
55 }
56​
57 return <div>{new Date().toISOString().replace('T', ' ').split('.')[0]}</div>
59​
60// ...fetchPosts
61// posts
62// ...5000
63// timestamp