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

usePosts.ts

22 lines510 Bytes

1import { useQuery } from '@tanstack/react-query'
2​
3type Post = {
4 id: number
5 title: string
6 body: string
7}
8​
9const fetchPosts = async (limit = 10): Promise<Array<Post>> => {
10 const response = await fetch('https://jsonplaceholder.typicode.com/posts')
11 const data = await response.json()
12 return data.filter((x: Post) => x.id <= limit)
14​
15const usePosts = (limit: number) => {
16 return useQuery({
17 queryKey: ['posts', limit],
18 queryFn: () => fetchPosts(limit),
19 })
21​
22export { usePosts, fetchPosts }