File size: 2,785 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { useQuery } from '@tanstack/solid-query'
import { Suspense, createSignal } from 'solid-js'
import { NoHydration } from 'solid-js/web'
import { Title } from '@solidjs/meta'
import type { UseQueryResult } from '@tanstack/solid-query'
import { fetchUser } from '~/utils/api'

export default function Hydration() {
  const query = useQuery(() => ({
    queryKey: ['user'],
    queryFn: () => fetchUser({ sleep: 500 }),
    deferStream: true,
  }))

  const [initialQueryState] = createSignal(JSON.parse(JSON.stringify(query)))

  return (
    <main>
      <Title>Solid Query - Hydration</Title>

      <h1>Solid Query - Hydration Example</h1>

      <div class="description">
        <p>
          Lists the query state as seen on the server, initial render on the
          client (right after hydration), and current client value. Ideally, if
          SSR is setup correctly, these values are exactly the same in all three
          contexts.
        </p>
      </div>

      <button onClick={() => query.refetch()}>Refetch</button>

      <table class="example example--table">
        <thead>
          <tr>
            <th>Context</th>
            <th>data.name</th>
            <th>isFetching</th>
            <th>isFetched</th>
            <th>isPending</th>
            <th>isRefetching</th>
            <th>isLoading</th>
            <th>isStale</th>
            <th>isSuccess</th>
            <th>isError</th>
            <th>error</th>
            <th>fetchStatus</th>
            <th>dataUpdatedAt</th>
          </tr>
        </thead>

        <tbody>
          <Suspense>
            <NoHydration>
              <QueryStateRow context="server" query={query} />
            </NoHydration>

            <QueryStateRow
              context="client (initial render)"
              query={initialQueryState()!}
            />

            <QueryStateRow context="client" query={query} />
          </Suspense>
        </tbody>
      </table>
    </main>
  )
}

type QueryState = UseQueryResult<
  {
    id: string
    name: string
    queryTime: number
  },
  Error
>

const QueryStateRow = (props: { context: string; query: QueryState }) => {
  return (
    <tr>
      <td>{props.context}</td>
      <td>{props.query.data?.name}</td>
      <td>{String(props.query.isFetching)}</td>
      <td>{String(props.query.isFetched)}</td>
      <td>{String(props.query.isPending)}</td>
      <td>{String(props.query.isRefetching)}</td>
      <td>{String(props.query.isLoading)}</td>
      <td>{String(props.query.isStale)}</td>
      <td>{String(props.query.isSuccess)}</td>
      <td>{String(props.query.isError)}</td>
      <td>{String(props.query.error)}</td>
      <td>{String(props.query.fetchStatus)}</td>
      <td>{String(props.query.dataUpdatedAt)}</td>
    </tr>
  )
}