Open History to see the last commit
1import React from 'react'
2import ReactDOM from 'react-dom/client'
3import {
4 QueryClient,
5 QueryClientProvider,
6 useQuery,
7} from '@tanstack/react-query'
8import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
9β
10const queryClient = new QueryClient()
11β
12export default function App() {
13 return (
14 <QueryClientProvider client={queryClient}>
15 <ReactQueryDevtools />
16 <Example />
17 </QueryClientProvider>
18 )
19}
20β
21function Example() {
22 const { isPending, error, data, isFetching } = useQuery({
23 queryKey: ['repoData'],
24 queryFn: async () => {
25 const response = await fetch(
26 'https://api.github.com/repos/TanStack/query',
27 )
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 )
46}
47β
48const rootElement = document.getElementById('root')
49if (!rootElement) throw new Error('Missing #root element')
50ReactDOM.createRoot(rootElement).render(<App />)