Open History to see the last commit
1/* @refresh reload */
2import type { QueryFunction } from '@tanstack/solid-query'
3import {
4 createQuery,
5 QueryClient,
6 QueryClientProvider,
7 useQueryClient,
8} from '@tanstack/solid-query'
9import type { Setter } from 'solid-js'
10import { createSignal, For, Match, Show, Switch } from 'solid-js'
11import { render } from 'solid-js/web'
12β
13// Define a default query function that will receive the query key
14const defaultQueryFn: QueryFunction<unknown> = async ({ queryKey }) => {
15 const response = await fetch(
16 `https://jsonplaceholder.typicode.com${queryKey[0]}`,
17 {
18 method: 'GET',
19 },
20 )
21 return response.json()
22}
23β
24// provide the default query function to your app via the query client
25const queryClient = new QueryClient({
26 defaultOptions: {
27 queries: {
28 queryFn: defaultQueryFn,
29 },
30 },
31})
32β
33function App() {
34 const [postId, setPostId] = createSignal(-1)
35β
36 return (
37 <QueryClientProvider client={queryClient}>
38 <p>
39 As you visit the posts below, you will notice them in a loading state
40 the first time you load them. However, after you return to this list and
41 click on any posts you have already visited again, you will see them
42 load instantly and background refresh right before your eyes!{' '}
43 <strong>
44 (You may need to throttle your network speed to simulate longer
45 loading sequences)
46 </strong>
47 </p>
48 <Show when={postId() > -1} fallback={<Posts setPostId={setPostId} />}>
49 <Post postId={postId()} setPostId={setPostId} />
50 </Show>
51 </QueryClientProvider>
52 )
53}
54β
55function Posts(props: { setPostId: Setter<number> }) {
56 const queryClient = useQueryClient()
57β
58 // All you have to do now is pass a key!
59 const state = createQuery<any[]>({ queryKey: () => ['/posts'] })
60β
61 return (
62 <div>
63 <h1>Posts</h1>
64 <div>
65 <Switch>
66 <Match when={state.status === 'loading'}>Loading...</Match>
67 <Match when={state.status === 'error'}>
68 <span>Error: {(state.error as Error).message}</span>
69 </Match>
70 <Match when={state.data !== undefined}>
71 <>
72 <div>
73 <For each={state.data}>
74 {(post) => (
75 <p>
76 <a
77 onClick={() => props.setPostId(post.id)}
78 href="#"
79 style={
80 // We can use the queryCache here to show bold links for
81 // ones that are cached
82 queryClient.getQueryData(['post', post.id])
83 ? {
84 'font-weight': 'bold',
85 color: 'green',
86 }
87 : {}
88 }
89 >
90 {post.title}
91 </a>
92 </p>
93 )}
94 </For>
95 </div>
96 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
97 </>
98 </Match>
99 </Switch>
100 </div>
101 </div>
102 )
103}
104β
105function Post(props: { postId: number; setPostId: Setter<number> }) {
106 // You can even leave out the queryFn and just go straight into options
107 const state = createQuery<any>({
108 queryKey: () => [`/posts/${props.postId}`],
109 enabled: !!props.postId,
110 })
111β
112 return (
113 <div>
114 <div>
115 <a onClick={() => props.setPostId(-1)} href="#">
116 Back
117 </a>
118 </div>
119 <Switch>
120 <Match when={!props.postId || state.status === 'loading'}>
121 Loading...
122 </Match>
123 <Match when={state.status === 'error'}>
124 <span>Error: {(state.error as Error).message}</span>
125 </Match>
126 <Match when={state.data !== undefined}>
127 <>
128 <h1>{state.data.title}</h1>
129 <div>
130 <p>{state.data.body}</p>
131 </div>
132 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
133 </>
134 </Match>
135 </Switch>
136 </div>
137 )
138}
139β
140render(() => <App />, document.getElementById('root') as HTMLElement)