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

client-component.tsx

36 lines738 Bytes

1'use client'
2​
3import { Temporal } from '@js-temporal/polyfill'
4import { useQuery } from '@tanstack/react-query'
5import React from 'react'
6​
7export function ClientComponent() {
8 const query = useQuery({
9 queryKey: ['data'],
10 queryFn: async () => {
11 const { count } = await (
12 await fetch('http://localhost:3000/count')
13 ).json()
14​
15 return {
16 text: 'data from client',
17 date: Temporal.PlainDate.from('2023-01-01'),
18 count,
19 }
20 },
21 })
22​
23 if (query.isPending) {
24 return <div>Loading...</div>
25 }
26​
27 if (query.isError) {
28 return <div>An error has occurred!</div>
29 }
30​
31 return (
32 <div>
33 {query.data.text} - {query.data.date.toJSON()} - {query.data.count}
34 </div>
35 )