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