import { Await, createFileRoute, notFound } from '@tanstack/react-router' import * as React from 'react' import type { PostType } from './route' async function fetchPostById(postId: string) { console.info(`Fetching post with id ${postId}...`) await new Promise((r) => setTimeout(r, 100 + Math.round(Math.random() * 100))) const res = await fetch( `https://jsonplaceholder.typicode.com/posts/${postId}`, ) if (res.status === 404) throw notFound() return res.json() as Promise } export type CommentType = { id: string postId: string name: string email: string body: string } async function fetchComments(postId: string) { await new Promise((r) => setTimeout(r, 2000)) return fetch( `https://jsonplaceholder.typicode.com/comments?postId=${postId}`, ).then((r) => r.json() as Promise>) } export const Route = createFileRoute('/posts/$postId')({ loader: async ({ params: { postId } }) => { const commentsPromise = fetchComments(postId) const post = await fetchPostById(postId) return { post, commentsPromise: commentsPromise, } }, wrapInSuspense: true, errorComponent: ({ error }) => { return
Failed to load post: {(error as any).message}
}, notFoundComponent: () => { return
Post not found
}, component: PostComponent, }) function PostComponent() { const { post, commentsPromise } = Route.useLoaderData() return (

{post.title}

{post.body}
Loading comments...
} key={post.id} > {(comments) => { return (
Comments
{comments.map((comment) => { return (
{comment.name}
{comment.email}
{comment.body}
) })}
) }} ) }