Skip to content

Commit

Permalink
docs: add clear method
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Sep 1, 2024
1 parent cf46ded commit 9a0bf4f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Use these composables in your templates or components:

```vue
<script setup lang="ts">
const { data, pending, refresh, error } = await useJsonPlaceholderData('posts/1')
const { data, refresh, error, status, clear } = await useJsonPlaceholderData('posts/1')
</script>
<template>
Expand Down
15 changes: 13 additions & 2 deletions docs/api/use-fetch-like.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The composable supports every [`useAsyncData` option](https://nuxt.com/docs/api/
- `data`: the result of the asynchronous function that is passed in.
- `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"`).
- `status`: a string indicating the status of the data request (`'idle'`, `'pending'`, `'success'`, `'error'`).
- `clear`: a function which will set `data` to `undefined`, set `error` to `null`, set `status` to `'idle'`, and mark any currently pending requests as cancelled.

By default, Nuxt waits until a `refresh` is finished before it can be executed again.
Expand Down Expand Up @@ -91,6 +91,17 @@ const { data } = await useMyApiData('posts', {
The key can be a reactive value, e.g. a computed property.
:::

To clear the cache for a specific request, use the `clear` function. You can then call `refresh` to fetch the data again:

```ts
const { data, refresh, clear } = await useMyApiData('posts')

async function invalidateAndRefresh() {
clear()
await refresh()
}
```
## Examples
::: info
Expand All @@ -117,7 +128,7 @@ export default defineNuxtConfig({
```vue
<script setup lang="ts">
const { data, pending, error, refresh } = await useJsonPlaceholderData('posts/1')
const { data, refresh, error, status, clear } = await useJsonPlaceholderData('posts/1')
</script>

<template>
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Use these composables in your templates or components:

```vue
<script setup lang="ts">
const { data, pending, refresh, error } = await useJsonPlaceholderData('posts/1')
const { data, refresh, error, status, clear } = await useJsonPlaceholderData('posts/1')
</script>
<template>
Expand Down
6 changes: 2 additions & 4 deletions playground/pages/jsonPlaceholder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { NuxtError } from '#app'
const route = useRoute()
// Intended for similar use cases as `useFetch`
const { data, pending, error } = await useJsonPlaceholderData<JsonPlaceholderComment>(
const { data, status, error } = await useJsonPlaceholderData<JsonPlaceholderComment>(
'comments',
{
query: computed(() => ({
Expand Down Expand Up @@ -72,9 +72,7 @@ async function onSubmit() {
<h2>useJsonPlaceholderData</h2>
<p>Responses are cached by default.</p>
<p>
Status:
<mark v-if="pending">pending</mark>
<code v-else>fetched</code>
Status: <mark>{{ status }}</mark>
</p>
<p>
<button @click="incrementPostId()">
Expand Down

0 comments on commit 9a0bf4f

Please sign in to comment.