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

index.tsx

53 lines1.4 KB

1/* @refresh reload */
2import {
3 createQuery,
4 QueryClient,
5 QueryClientProvider,
6} from '@tanstack/solid-query'
7import { Match, Switch } from 'solid-js'
8import { render } from 'solid-js/web'
9​
10const queryClient = new QueryClient()
11​
12export default function App() {
13 return (
14 <QueryClientProvider client={queryClient}>
15 <Example />
16 </QueryClientProvider>
17 )
19​
20function Example() {
21 const state = createQuery(() => ({
22 queryKey: ['repoData'],
23 queryFn: async () => {
24 const response = await fetch(
25 'https://api.github.com/repos/tannerlinsley/react-query',
26 {
27 method: 'GET',
28 },
29 )
30 return 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 )
53render(() => <App />, document.getElementById('root') as HTMLElement)