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

Function Posts

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

Source from the content-addressed store, hash-verified

69}
70
71function Posts({
72 setPostId,
73}: {
74 setPostId: React.Dispatch<React.SetStateAction<number>>
75}) {
76 const queryClient = useQueryClient()
77 const { status, data, error, isFetching } = usePosts()
78
79 return (
80 <div>
81 <h1>Posts</h1>
82 <div>
83 {status === 'pending' ? (
84 'Loading...'
85 ) : status === 'error' ? (
86 <span>Error: {error.message}</span>
87 ) : (
88 <>
89 <div>
90 {data.map((post) => (
91 <p key={post.id}>
92 <a
93 onClick={() => setPostId(post.id)}
94 href="#"
95 style={
96 // We can find the existing query data here to show bold links for
97 // ones that are cached
98 queryClient.getQueryData(['post', post.id])
99 ? {
100 fontWeight: 'bold',
101 color: 'green',
102 }
103 : {}
104 }
105 >
106 {post.title}
107 </a>
108 </p>
109 ))}
110 </div>
111 <div>{isFetching ? 'Background Updating...' : ' '}</div>
112 </>
113 )}
114 </div>
115 </div>
116 )
117}
118
119function usePost(postId: number) {
120 return useQuery({

Callers

nothing calls this directly

Calls 3

useQueryClientFunction · 0.90
usePostsFunction · 0.70
getQueryDataMethod · 0.45

Tested by

no test coverage detected