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

$postId-loader.tsx

18 lines473 Bytes

1import { useMatch } from '@tanstack/react-router';
2export type PostType = {
3 id: string;
4 title: string;
5 body: string;
6};
7export const loader = async ({
8 params: {
9 postId
10 }
11}) => {
12 console.log(`Fetching post with id ${postId}...`);
13 await new Promise(r => setTimeout(r, Math.round(Math.random() * 300)));
14 const post = await fetch(`https://jsonplaceholder.typicode.com/posts/${postId}`).then(r => (r.json() as Promise<PostType>));
15 return {
16 post
17 };
18};