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

Function Post

examples/preact/default-query-function/src/index.tsx:109–144  ·  view source on GitHub ↗
({
  postId,
  setPostId,
}: {
  postId: number
  setPostId: (postId: number) => void
})

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

useQueryFunction · 0.90

Tested by

no test coverage detected