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

DogList.tsx

34 lines676 Bytes

1import { useQuery } from '@tanstack/react-query'
2​
3type DogsResp = {
4 message: {
5 [dog: string]: string[]
6 }
7}
8​
9export const DogList = () => {
10 const { data, status } = useQuery<DogsResp>({
11 queryKey: ['dogs'],
12 queryFn: async () => {
13 const resp = await fetch('https://dog.ceo/api/breeds/list/all')
14 if (resp.ok) {
15 return resp.json()
16 }
17 throw new Error('something went wrong')
18 },
19 })
20​
21 if (status === 'pending') return 'Loading...'
22​
23 if (status === 'error') return 'Error!'
24​
25 const dogs = Object.keys(data?.message)
26​
27 return (
28 <div>
29 {dogs.map((dog) => (
30 <div key={dog}>{dog}</div>
31 ))}
32 </div>
33 )