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

index.tsx

49 lines1.2 KB

1import {
2 QueryClient,
3 QueryClientProvider,
4 useQuery,
5} from '@tanstack/preact-query'
6import { render } from 'preact'
7​
8const queryClient = new QueryClient()
9​
10export function App() {
11 return (
12 <QueryClientProvider client={queryClient}>
13 <Example />
14 </QueryClientProvider>
15 )
17​
18const Example = () => {
19 const { isPending, error, data, isFetching } = useQuery({
20 queryKey: ['repoData'],
21 queryFn: async () => {
22 const response = await fetch(
23 'https://api.github.com/repos/TanStack/query',
24 )
25 if (!response.ok) throw new Error('Failed to fetch repo data')
26 return await response.json()
27 },
28 })
29​
30 if (isPending) return 'Loading...'
31​
32 if (error !== null) return `An error has occurred: ${error.message}`
33​
34 return (
35 <div>
36 <h1>{data.full_name}</h1>
37 <p>{data.description}</p>
38 <strong>πŸ‘€ {data.subscribers_count}</strong>{' '}
39 <strong>✨ {data.stargazers_count}</strong>{' '}
40 <strong>🍴 {data.forks_count}</strong>
41 <div>{isFetching ? 'Updating...' : ''}</div>
42 </div>
43 )
45​
46const app = document.getElementById('app')
47if (!app) throw new Error('Missing #app element')
48​
49render(<App />, app)