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

Function Post

examples/react/default-query-function/src/index.tsx:112–147  ·  view source on GitHub ↗
({
  postId,
  setPostId,
}: {
  postId: number
  setPostId: React.Dispatch<React.SetStateAction<number>>
})

Source from the content-addressed store, hash-verified

110}
111
112function Post({
113 postId,
114 setPostId,
115}: {
116 postId: number
117 setPostId: React.Dispatch<React.SetStateAction<number>>
118}) {
119 // You can even leave out the queryFn and just go straight into options
120 const { status, data, error, isFetching } = useQuery<Post>({
121 queryKey: [`/posts/${postId}`],
122 enabled: !!postId,
123 })
124
125 return (
126 <div>
127 <div>
128 <a onClick={() => setPostId(-1)} href="#">
129 Back
130 </a>
131 </div>
132 {!postId || status === 'pending' ? (
133 'Loading...'
134 ) : status === 'error' ? (
135 <span>Error: {error.message}</span>
136 ) : (
137 <>
138 <h1>{data.title}</h1>
139 <div>
140 <p>{data.body}</p>
141 </div>
142 <div>{isFetching ? 'Background Updating...' : ' '}</div>
143 </>
144 )}
145 </div>
146 )
147}
148
149const rootElement = document.getElementById('root')
150if (!rootElement) throw new Error('Missing #root element')

Callers

nothing calls this directly

Calls 1

useQueryFunction · 0.90

Tested by

no test coverage detected