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

Function Post

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

useQueryFunction · 0.90

Tested by

no test coverage detected