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

hydration.tsx

99 lines2.7 KB

1import { useQuery } from '@tanstack/solid-query'
2import { Suspense, createSignal } from 'solid-js'
3import { NoHydration } from '@solidjs/web'
4import { Title } from '@solidjs/meta'
5import type { UseQueryResult } from '@tanstack/solid-query'
6import { fetchUser } from '~/utils/api'
7​
8export default function Hydration() {
9 const query = useQuery(() => ({
10 queryKey: ['user'],
11 queryFn: () => fetchUser({ sleep: 500 }),
12 deferStream: true,
13 }))
14​
15 const [initialQueryState] = createSignal(JSON.parse(JSON.stringify(query)))
16​
17 return (
18 <main>
19 <Title>Solid Query - Hydration</Title>
20​
21 <h1>Solid Query - Hydration Example</h1>
22​
23 <div class="description">
24 <p>
25 Lists the query state as seen on the server, initial render on the
26 client (right after hydration), and current client value. Ideally, if
27 SSR is setup correctly, these values are exactly the same in all three
28 contexts.
29 </p>
30 </div>
31​
32 <button onClick={() => query.refetch()}>Refetch</button>
33​
34 <table class="example example--table">
35 <thead>
36 <tr>
37 <th>Context</th>
38 <th>data.name</th>
39 <th>isFetching</th>
40 <th>isFetched</th>
41 <th>isPending</th>
42 <th>isRefetching</th>
43 <th>isLoading</th>
44 <th>isStale</th>
45 <th>isSuccess</th>
46 <th>isError</th>
47 <th>error</th>
48 <th>fetchStatus</th>
49 <th>dataUpdatedAt</th>
50 </tr>
51 </thead>
52​
53 <tbody>
54 <Suspense>
55 <NoHydration>
56 <QueryStateRow context="server" query={query} />
57 </NoHydration>
58​
59 <QueryStateRow
60 context="client (initial render)"
61 query={initialQueryState()!}
62 />
63​
64 <QueryStateRow context="client" query={query} />
65 </Suspense>
66 </tbody>
67 </table>
68 </main>
69 )
71​
72type QueryState = UseQueryResult<
73 {
74 id: string
75 name: string
76 queryTime: number
77 },
78 Error
80​
81const QueryStateRow = (props: { context: string; query: QueryState }) => {
82 return (
83 <tr>
84 <td>{props.context}</td>
85 <td>{props.query.data?.name}</td>
86 <td>{String(props.query.isFetching)}</td>
87 <td>{String(props.query.isFetched)}</td>
88 <td>{String(props.query.isPending)}</td>
89 <td>{String(props.query.isRefetching)}</td>
90 <td>{String(props.query.isLoading)}</td>
91 <td>{String(props.query.isStale)}</td>
92 <td>{String(props.query.isSuccess)}</td>
93 <td>{String(props.query.isError)}</td>
94 <td>{String(props.query.error)}</td>
95 <td>{String(props.query.fetchStatus)}</td>
96 <td>{String(props.query.dataUpdatedAt)}</td>
97 </tr>
98 )