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

index.tsx

49 lines1.2 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 return await response.json()
28 },
29 })
30​
31 if (isPending) return 'Loading...'
32​
33 if (error) return 'An error has occurred: ' + error.message
34​
35 return (
36 <div>
37 <h1>{data.full_name}</h1>
38 <p>{data.description}</p>
39 <strong>πŸ‘€ {data.subscribers_count}</strong>{' '}
40 <strong>✨ {data.stargazers_count}</strong>{' '}
41 <strong>🍴 {data.forks_count}</strong>
42 <div>{isFetching ? 'Updating...' : ''}</div>
43 </div>
44 )
46​
47const rootElement = document.getElementById('root')
48if (!rootElement) throw new Error('Missing #root element')
49ReactDOM.createRoot(rootElement).render(<App />)