docs: update return for useFetch and useAsyncData #24407 (#24775)

This commit is contained in:
Victor Akintunde 2023-12-18 19:37:51 +01:00 committed by GitHub
parent 1f8aac0160
commit 5d2ac28576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 0 deletions

View File

@ -143,6 +143,26 @@ const { data: discounts, pending } = await useAsyncData('cart-discount', async (
Read more about `useAsyncData`.
::
## Return Values
`useFetch` and `useAsyncData` have the same return values listed below.
- `data`: the result of the asynchronous function that is passed in.
- `pending`: a boolean indicating whether the data is still being fetched.
- `refresh`/`execute`: a function that can be used to refresh the data returned by the `handler` function.
- `error`: an error object if the data fetching failed.
- `status`: a string indicating the status of the data request (`"idle"`, `"pending"`, `"success"`, `"error"`).
::callout
`data`, `pending`, `error` and `status` are Vue refs accessible with `.value` in `<script setup>`
::
By default, Nuxt waits until a `refresh` is finished before it can be executed again.
::callout
If you have not fetched data on the server (for example, with `server: false`), then the data _will not_ be fetched until hydration completes. This means even if you await `useFetch` on client-side, `data` will remain null within `<script setup>`.
::
## Options
[`useAsyncData`](/docs/api/composables/use-async-data) and [`useFetch`](/docs/api/composables/use-fetch) return the same object type and accept a common set of options as their last argument. They can help you control the composables behavior, such as navigation blocking, caching or execution.

View File

@ -25,6 +25,10 @@ const { data, pending, error, refresh } = await useAsyncData(
</script>
```
::callout
`data`, `pending`, `status` and `error` are Vue refs and they should be accessed with `.value` when used within the `<script setup>`, while `refresh`/`execute` is a plain function for refetching data.
::
### Watch Params
The built-in `watch` option allows automatically rerunning the fetcher function when any changes are detected.

View File

@ -27,6 +27,10 @@ const { data, pending, error, refresh } = await useFetch(`https://api.nuxtjs.dev
</script>
```
::callout
`data`, `pending`, `status` and `error` are Vue refs and they should be accessed with `.value` when used within the `<script setup>`, while `refresh`/`execute` is a plain function for refetching data.
::
Using the `query` option, you can add search parameters to your query. This option is extended from [unjs/ofetch](https://github.com/unjs/ofetch) and is using [unjs/ufo](https://github.com/unjs/ufo) to create the URL. Objects are automatically stringified.
```ts