docs: add another example for `useAsyncData` (#19225)

This commit is contained in:
Jakub Zomerfeld 2023-03-20 00:51:18 +01:00 committed by GitHub
parent 97ae4e23f9
commit 801aba830e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

View File

@ -80,6 +80,24 @@ const { data, pending, error, refresh } = await useAsyncData(
)
```
## Example with watching params change
The built-in `watch` option allows automatically rerunning the fetcher function when any changes are detected.
```ts
const page = ref(1)
const { data: posts } = await useAsyncData(
'posts',
() => $fetch('https://fakeApi.com/posts', {
params: {
page: page.value
}
}), {
watch: [page]
}
)
```
::alert{type=warning}
`useAsyncData` is a reserved function name transformed by the compiler, so you should not name your own function `useAsyncData`.
::