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

Function Posts

examples/react/basic-graphql-request/src/index.tsx:39–87  ·  view source on GitHub ↗
({
  setPostId,
}: {
  setPostId: React.Dispatch<React.SetStateAction<string | null>>
})

Source from the content-addressed store, hash-verified

37}
38
39function Posts({
40 setPostId,
41}: {
42 setPostId: React.Dispatch<React.SetStateAction<string | null>>
43}) {
44 const queryClient = useQueryClient()
45 const { status, data, error, isFetching } = useQuery(postsQueries.list())
46
47 return (
48 <div>
49 <h1>Posts</h1>
50 <div>
51 {status === 'pending' ? (
52 'Loading...'
53 ) : status === 'error' ? (
54 <span>Error: {error.message}</span>
55 ) : (
56 <>
57 <div>
58 {data.map((post) => (
59 <p key={post.id}>
60 <a
61 onClick={() => setPostId(post.id)}
62 href="#"
63 style={
64 // We can find the existing query data here to show bold links for
65 // ones that are cached
66 queryClient.getQueryData(
67 postsQueries.detail(post.id).queryKey,
68 )
69 ? {
70 fontWeight: 'bold',
71 color: 'green',
72 }
73 : {}
74 }
75 >
76 {post.title}
77 </a>
78 </p>
79 ))}
80 </div>
81 <div>{isFetching ? 'Background Updating...' : ' '}</div>
82 </>
83 )}
84 </div>
85 </div>
86 )
87}
88
89function Post({
90 postId,

Callers

nothing calls this directly

Calls 3

useQueryClientFunction · 0.90
useQueryFunction · 0.90
getQueryDataMethod · 0.45

Tested by

no test coverage detected