useQuery
The useQuery
hook executes an asynchronous operation like fetch
in a way that supports Suspense. It's based on react-query. You can use this hook to call any third-party APIs from a server component.
Example code
Anchor link to section titled "Example code"
The useQuery
hook takes the following arguments:
Key | Required | Description |
---|---|---|
key |
Yes | A string or an array to uniquely identify the query. |
queryFn |
Yes | An asynchronous query function like fetch which returns data. |
queryOptions |
No | The options to manage the cache behavior of the sub-request. |
The queryOptions
object accepts the following properties:
Key | Required | Description |
---|---|---|
cache |
No | The caching strategy to help you determine which cache control header to set. |
preload |
No | Whether to preload the request. It defaults to true only when the CachingStrategy is not CacheNone . Specify false to disable or use '*' to preload the query for all requests. |
shouldCacheResponse |
No | A function that inspects the response body to determine if it should be cached. |
Return value
Anchor link to section titled "Return value"The useQuery
returns an object with the following key:
Key | Description |
---|---|
data |
The data returned by the query. |