Best way to append new data to array from useAsyncData
practiceAreasData
is an array. i have a form to create a new practice area. how can i append the newly created data without fetching all practice areas2 Replies
Hi @the_aceix. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The best way to append new data to an array returned by
useAsyncData
—without refetching all data—is to directly mutate the reactive data
ref. This is a common pattern in Nuxt, especially when implementing features like Optimistic Updates.
Assuming your practiceAreasData
is a ref containing an array, you can append a new item like this:
Or, if you want to mutate the array in place (which is also reactive):
This approach is documented in the Nuxt docs under Optimistic Updates, where the cached data is updated immediately after a mutation, and only refetched if needed (e.g., if the server operation fails) Nuxt 4.x useNuxtData.
Example pattern:
This lets you append new data to the array immediately, providing a responsive UI, and only refetch if necessary. This pattern is recommended in the Nuxt documentation for similar use cases Nuxt 4.x useNuxtData.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@448141117368827905> Kapa.ai is still learning and improving, please let me know how I did by reacting below.