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

index.tsx

50 lines1.3 KB

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