MCPcopy Create free account
hub / github.com/TanStack/query / Posts

Function Posts

examples/preact/basic/src/index.tsx:21–65  ·  view source on GitHub ↗
({ setPostId }: { setPostId: (postId: number) => void })

Source from the content-addressed store, hash-verified

19})
20
21function Posts({ setPostId }: { setPostId: (postId: number) => void }) {
22 const queryClient = useQueryClient()
23 const { status, data, error, isFetching } = useQuery(postsQueries.list())
24
25 return (
26 <div>
27 <h1>Posts</h1>
28 <div>
29 {status === 'pending' ? (
30 'Loading...'
31 ) : status === 'error' ? (
32 <span>Error: {error.message}</span>
33 ) : (
34 <>
35 <div>
36 {data.map((post) => (
37 <p key={post.id}>
38 <a
39 onClick={() => setPostId(post.id)}
40 href="#"
41 style={
42 // We can access the query data here to show bold links for
43 // ones that are cached
44 queryClient.getQueryData(
45 postsQueries.detail(post.id).queryKey,
46 )
47 ? {
48 fontWeight: 'bold',
49 color: 'green',
50 }
51 : {}
52 }
53 >
54 {post.title}
55 </a>
56 </p>
57 ))}
58 </div>
59 <div>{isFetching ? 'Background Updating...' : ' '}</div>
60 </>
61 )}
62 </div>
63 </div>
64 )
65}
66
67function Post({
68 postId,

Callers

nothing calls this directly

Calls 3

useQueryClientFunction · 0.90
useQueryFunction · 0.90
getQueryDataMethod · 0.45

Tested by

no test coverage detected