Skip to content

Commit

Permalink
docs: vue behavior when modifying cached arrays (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinz01 authored Oct 31, 2024
1 parent 1ab451d commit f825937
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/src/guide/invalidating-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ This will update the `list-posts` cache at the client side, making it equal to t
When operations like this are possible to be made, they are the preferred. That's because
we do not contact the server again and update ourselves the cache.

::: tip

**Note to Vue users:** If you modify an array as shown above and then assign the result
data of the axios request to a Vue `ref`, you may have issues with the UI not updating.
This is because the cached array is the same object as was returned from the previous request.
You need to copy the array before modifying it:
```ts
listPostsCache.data.posts = [...listPostsCache.data.posts]
listPostsCache.data.posts.push(createPostResponse.data);
// or
listPostsCache.data.posts = [...listPostsCache.data.posts, createPostResponse.data]
```
or before assigning it to the `ref`:
```ts
myRef.value = [...axios.get(url).data]
```

:::

## Through network

Sometimes, the mutation you made is not simple enough and would need a lot of copied
Expand Down

0 comments on commit f825937

Please sign in to comment.