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

index.tsx

138 lines4.0 KB

1/* @refresh reload */
2import {
3 createQuery,
4 QueryClient,
5 QueryClientProvider,
6 QueryFunction,
7 useQueryClient,
8} from '@tanstack/solid-query'
9import { createSignal, For, Match, Setter, 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()
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 )
53​
54function Posts(props: { setPostId: Setter<number> }) {
55 const queryClient = useQueryClient()
56​
57 // All you have to do now is pass a key!
58 const state = createQuery<any[]>(() => ['/posts'])
59​
60 return (
61 <div>
62 <h1>Posts</h1>
63 <div>
64 <Switch>
65 <Match when={state.status === 'loading'}>Loading...</Match>
66 <Match when={state.status === 'error'}>
67 <span>Error: {(state.error as Error).message}</span>
68 </Match>
69 <Match when={state.data !== undefined}>
70 <>
71 <div>
72 <For each={state.data}>
73 {(post) => (
74 <p>
75 <a
76 onClick={() => props.setPostId(post.id)}
77 href="#"
78 style={
79 // We can use the queryCache here to show bold links for
80 // ones that are cached
81 queryClient.getQueryData(['post', post.id])
82 ? {
83 'font-weight': 'bold',
84 color: 'green',
85 }
86 : {}
87 }
88 >
89 {post.title}
90 </a>
91 </p>
92 )}
93 </For>
94 </div>
95 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
96 </>
97 </Match>
98 </Switch>
99 </div>
100 </div>
101 )
103​
104function Post(props: { postId: number; setPostId: Setter<number> }) {
105 // You can even leave out the queryFn and just go straight into options
106 const state = createQuery<any>(() => [`/posts/${props.postId}`], {
107 enabled: !!props.postId,
108 })
109​
110 return (
111 <div>
112 <div>
113 <a onClick={() => props.setPostId(-1)} href="#">
114 Back
115 </a>
116 </div>
117 <Switch>
118 <Match when={!props.postId || state.status === 'loading'}>
119 Loading...
120 </Match>
121 <Match when={state.status === 'error'}>
122 <span>Error: {(state.error as Error).message}</span>
123 </Match>
124 <Match when={state.data !== undefined}>
125 <>
126 <h1>{state.data.title}</h1>
127 <div>
128 <p>{state.data.body}</p>
129 </div>
130 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
131 </>
132 </Match>
133 </Switch>
134 </div>
135 )
137​
138render(() => <App />, document.getElementById('root') as HTMLElement)