|
| 1 | +import type { CreateQueryResult } from '@tanstack/solid-query' |
| 2 | +import { createQuery } from '@tanstack/solid-query' |
| 3 | +import { createSignal, Suspense } from 'solid-js' |
| 4 | +import { fetchUser } from '~/utils/api' |
| 5 | +import { NoHydration } from 'solid-js/web' |
| 6 | +import { Title } from 'solid-start' |
| 7 | + |
| 8 | +export default function Hydration() { |
| 9 | + const query = createQuery(() => ({ |
| 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>fetchStatus</th> |
| 47 | + <th>dataUpdatedAt</th> |
| 48 | + </tr> |
| 49 | + </thead> |
| 50 | + |
| 51 | + <tbody> |
| 52 | + <Suspense> |
| 53 | + <NoHydration> |
| 54 | + <QueryStateRow context="server" query={query} /> |
| 55 | + </NoHydration> |
| 56 | + |
| 57 | + <QueryStateRow |
| 58 | + context="client (initial render)" |
| 59 | + query={initialQueryState()!} |
| 60 | + /> |
| 61 | + |
| 62 | + <QueryStateRow context="client" query={query} /> |
| 63 | + </Suspense> |
| 64 | + </tbody> |
| 65 | + </table> |
| 66 | + </main> |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +type QueryState = CreateQueryResult< |
| 71 | + { |
| 72 | + id: string |
| 73 | + name: string |
| 74 | + queryTime: number |
| 75 | + }, |
| 76 | + Error |
| 77 | +> |
| 78 | + |
| 79 | +const QueryStateRow = (props: { context: string; query: QueryState }) => { |
| 80 | + return ( |
| 81 | + <tr> |
| 82 | + <td>{props.context}</td> |
| 83 | + <td>{props.query.data?.name}</td> |
| 84 | + <td>{String(props.query.isFetching)}</td> |
| 85 | + <td>{String(props.query.isFetched)}</td> |
| 86 | + <td>{String(props.query.isPending)}</td> |
| 87 | + <td>{String(props.query.isRefetching)}</td> |
| 88 | + <td>{String(props.query.isLoading)}</td> |
| 89 | + <td>{String(props.query.isStale)}</td> |
| 90 | + <td>{String(props.query.isSuccess)}</td> |
| 91 | + <td>{String(props.query.fetchStatus)}</td> |
| 92 | + <td>{String(props.query.dataUpdatedAt)}</td> |
| 93 | + </tr> |
| 94 | + ) |
| 95 | +} |
0 commit comments