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

index.tsx

52 lines1.4 KB

1/* @refresh reload */
2import {
3 QueryClient,
4 QueryClientProvider,
5 createQuery,
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 = createQuery(() => ({
24 queryKey: ['repoData'],
25 queryFn: async () => {
26 const response = await fetch(
27 'https://api.github.com/repos/TanStack/query',
28 )
29 return await response.json()
30 },
31 }))
32​
33 return (
34 <Switch>
35 <Match when={state.isPending}>Loading...</Match>
36 <Match when={state.error}>
37 {'An error has occurred: ' + (state.error as Error).message}
38 </Match>
39 <Match when={state.data !== undefined}>
40 <div>
41 <h1>{state.data.name}</h1>
42 <p>{state.data.description}</p>
43 <strong>πŸ‘€ {state.data.subscribers_count}</strong>{' '}
44 <strong>✨ {state.data.stargazers_count}</strong>{' '}
45 <strong>🍴 {state.data.forks_count}</strong>
46 <div>{state.isFetching ? 'Updating...' : ''}</div>
47 </div>
48 </Match>
49 </Switch>
50 )
52render(() => <App />, document.getElementById('root') as HTMLElement)