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

page.tsx

43 lines1.1 KB

1import { Temporal } from '@js-temporal/polyfill'
2import { HydrationBoundary, dehydrate, noop } from '@tanstack/react-query'
3import { headers } from 'next/headers'
4import React from 'react'
5import { queryExampleAction } from './_action'
6import { ClientComponent } from './client-component'
7import { makeQueryClient } from './make-query-client'
8​
9export default function Home() {
10 const queryClient = makeQueryClient()
11​
12 queryClient
13 .query({
14 queryKey: ['data'],
15 queryFn: async () => {
16 const { count } = await (
17 await fetch('http://localhost:3000/count', {
18 headers: await headers(),
19 })
20 ).json()
21​
22 return {
23 text: 'data from server',
24 date: Temporal.PlainDate.from('2024-01-01'),
25 count,
26 }
27 },
28 })
29 .catch(noop)
30​
31 const state = dehydrate(queryClient)
32​
33 return (
34 <main>
35 <HydrationBoundary state={state}>
36 <ClientComponent />
37 </HydrationBoundary>
38 <form action={queryExampleAction}>
39 <button type="submit">Increment</button>
40 </form>
41 </main>
42 )