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

posts-loader.tsx

12 lines460 Bytes

1import { useMatch } from '@tanstack/react-router';
2import { PostType } from './posts/$postId';
3async function fetchPosts() {
4 console.log('Fetching posts...');
5 await new Promise(r => setTimeout(r, 300 + Math.round(Math.random() * 300)));
6 return fetch('https://jsonplaceholder.typicode.com/posts').then(d => (d.json() as Promise<PostType[]>)).then(d => d.slice(0, 10));
7}
8export const loader = async () => {
9 return {
10 posts: await fetchPosts()
11 };
12};