2022-04-06 05:56:08 +00:00
# `useFetch`
2022-07-25 12:37:39 +00:00
This composable provides a convenient wrapper around [`useAsyncData` ](/api/composables/use-async-data ) and [`$fetch` ](/api/utils/$fetch ). It automatically generates a key based on URL and fetch options, provides type hints for request url based on server routes, and infers API response type.
2022-04-13 17:14:03 +00:00
## Type
```ts [Signature]
function useFetch(
2022-07-07 16:26:04 +00:00
url: string | Request | Ref< string | Request > | () => string | Request,
options?: UseFetchOptions< DataT >
): Promise< AsyncData < DataT > >
2022-04-13 17:14:03 +00:00
type UseFetchOptions = {
2022-08-07 09:57:10 +00:00
key?: string
method?: string
params?: SearchParams
2022-05-20 09:04:37 +00:00
body?: RequestInit['body'] | Record< string , any >
2022-08-07 09:57:10 +00:00
headers?: { key: string, value: string }[]
baseURL?: string
2022-04-13 17:14:03 +00:00
server?: boolean
lazy?: boolean
default?: () => DataT
transform?: (input: DataT) => DataT
pick?: string[]
2022-06-22 10:41:34 +00:00
watch?: WatchSource[]
2022-07-14 21:30:05 +00:00
initialCache?: boolean
2022-04-13 17:14:03 +00:00
}
2022-07-07 16:26:04 +00:00
type AsyncData< DataT > = {
2022-04-13 17:14:03 +00:00
data: Ref< DataT >
pending: Ref< boolean >
refresh: () => Promise< void >
2022-04-29 18:42:22 +00:00
error: Ref< Error | boolean >
2022-04-13 17:14:03 +00:00
}
2022-04-06 05:56:08 +00:00
```
2022-04-13 17:14:03 +00:00
## Params
2022-04-06 05:56:08 +00:00
2022-08-07 09:57:10 +00:00
* **Url**: The URL to fetch.
2022-05-20 09:04:37 +00:00
* **Options (extends [unjs/ohmyfetch ](https://github.com/unjs/ohmyfetch ) options & [AsyncDataOptions ](/api/composables/use-async-data#params ))**:
2022-08-07 09:57:10 +00:00
* `method` : Request method.
* `params` : Query params.
2022-05-20 09:04:37 +00:00
* `body` : Request body - automatically stringified (if an object is passed).
2022-08-07 09:57:10 +00:00
* `headers` : Request headers.
* `baseURL` : Base URL for the request.
2022-04-13 17:14:03 +00:00
* **Options (from `useAsyncData` )**:
2022-08-07 09:57:10 +00:00
* `key` : a unique key to ensure that data fetching can be properly de-duplicated across requests, if not provided, it will be generated based on the `url` and fetch options.
2022-04-13 17:14:03 +00:00
* `lazy` : Whether to resolve the async function after loading the route, instead of blocking navigation (defaults to `false` ).
2022-04-16 13:53:36 +00:00
* `server` : Whether to fetch the data on the server (defaults to `true` ).
2022-04-13 17:14:03 +00:00
* `default` : A factory function to set the default value of the data, before the async function resolves - particularly useful with the `lazy: true` option.
2022-04-16 13:53:36 +00:00
* `pick` : Only pick specified keys in this array from the `handler` function result.
2022-08-07 09:57:10 +00:00
* `watch` : watch reactive sources to auto-refresh.
* `initialCache` : When set to `false` , will skip payload cache for initial fetch (defaults to `true` ).
2022-04-13 17:14:03 +00:00
* `transform` : A function that can be used to alter `handler` function result after resolving.
2022-07-07 16:26:04 +00:00
::alert{type=warning}
If you provide a function or ref as the `url` parameter, or if you provide functions as arguments to the `options` parameter, then the `useFetch` call will not match other `useFetch` calls elsewhere in your codebase, even if the options seem to be identical. If you wish to force a match, you may provide your own key in `options` .
::
2022-04-13 17:14:03 +00:00
## Return values
2022-04-06 05:56:08 +00:00
2022-08-07 09:57:10 +00:00
* **data**: the result of the asynchronous function that is passed in.
* **pending**: a boolean indicating whether the data is still being fetched.
* **refresh**: a function that can be used to refresh the data returned by the `handler` function.
* **error**: an error object if the data fetching failed.
2022-04-13 17:14:03 +00:00
By default, Nuxt waits until a `refresh` is finished before it can be executed again. Passing `true` as parameter skips that wait.
## Example
```ts
2022-05-09 14:12:41 +00:00
const { data, pending, error, refresh } = await useFetch(
2022-04-13 17:14:03 +00:00
'https://api.nuxtjs.dev/mountains',
{
pick: ['title']
}
)
```
2022-04-06 05:56:08 +00:00
2022-04-13 17:14:03 +00:00
:ReadMore{link="/guide/features/data-fetching"}
2022-04-06 05:56:08 +00:00
2022-04-13 17:14:03 +00:00
:LinkExample{link="/examples/composables/use-fetch"}