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

index.tsx

56 lines1.5 KB

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