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

Function Posts

examples/react/basic/src/index.tsx:21–69  ·  view source on GitHub ↗
({
  setPostId,
}: {
  setPostId: React.Dispatch<React.SetStateAction<number>>
})

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

useQueryClientFunction · 0.90
useQueryFunction · 0.90
getQueryDataMethod · 0.45

Tested by

no test coverage detected