2023-10-18 10:59:43 +00:00
---
title: 'useNuxtData'
description: 'Access the current cached value of data fetching composables.'
links:
- label: Source
icon: i-simple-icons-github
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/asyncData.ts
size: xs
---
2024-02-21 17:09:45 +00:00
::note
2023-07-07 16:24:09 +00:00
`useNuxtData` gives you access to the current cached value of [`useAsyncData` ](/docs/api/composables/use-async-data ) , `useLazyAsyncData` , [`useFetch` ](/docs/api/composables/use-fetch ) and [`useLazyFetch` ](/docs/api/composables/use-lazy-fetch ) with explicitly provided key.
2023-10-18 10:59:43 +00:00
::
2022-12-05 13:07:33 +00:00
2023-10-18 10:59:43 +00:00
## Usage
2022-12-05 13:07:33 +00:00
The example below shows how you can use cached data as a placeholder while the most recent data is being fetched from the server.
2023-10-18 10:59:43 +00:00
```vue [pages/posts.vue]
2024-02-13 12:50:38 +00:00
< script setup lang = "ts" >
2022-12-05 13:07:33 +00:00
// We can access same data later using 'posts' key
const { data } = await useFetch('/api/posts', { key: 'posts' })
2023-10-18 10:59:43 +00:00
< / script >
2022-12-05 13:07:33 +00:00
```
2024-02-13 12:50:38 +00:00
```vue [pages/posts/[id\\].vue]
< script setup lang = "ts" >
2023-10-18 10:59:43 +00:00
// Access to the cached value of useFetch in posts.vue (parent route)
const { id } = useRoute().params
2022-12-05 13:07:33 +00:00
const { data: posts } = useNuxtData('posts')
2023-10-18 10:59:43 +00:00
const { data } = useLazyFetch(`/api/posts/${id}`, {
key: `post-${id}` ,
default() {
2022-12-05 13:07:33 +00:00
// Find the individual post from the cache and set it as the default value.
2023-10-18 10:59:43 +00:00
return posts.value.find(post => post.id === id)
2022-12-05 13:07:33 +00:00
}
})
2024-02-13 12:50:38 +00:00
< / script >
2022-12-05 13:07:33 +00:00
```
2023-10-18 10:59:43 +00:00
## Optimistic Updates
2022-12-05 13:07:33 +00:00
We can leverage the cache to update the UI after a mutation, while the data is being invalidated in the background.
2023-10-18 10:59:43 +00:00
```vue [pages/todos.vue]
2024-02-13 12:50:38 +00:00
< script setup lang = "ts" >
2022-12-05 13:07:33 +00:00
// We can access same data later using 'todos' key
2023-10-18 10:59:43 +00:00
const { data } = await useAsyncData('todos', () => $fetch('/api/todos'))
< / script >
2022-12-05 13:07:33 +00:00
```
2023-10-18 10:59:43 +00:00
```vue [components/NewTodo.vue]
2024-02-13 12:50:38 +00:00
< script setup lang = "ts" >
2022-12-05 13:07:33 +00:00
const newTodo = ref('')
const previousTodos = ref([])
2024-11-14 10:48:11 +00:00
// Access to the cached value of useAsyncData in todos.vue
2022-12-05 13:07:33 +00:00
const { data: todos } = useNuxtData('todos')
const { data } = await useFetch('/api/addTodo', {
method: 'post',
body: {
todo: newTodo.value
},
onRequest () {
previousTodos.value = todos.value // Store the previously cached value to restore if fetch fails.
todos.value.push(newTodo.value) // Optimistically update the todos.
},
onRequestError () {
todos.value = previousTodos.value // Rollback the data if the request failed.
},
async onResponse () {
await refreshNuxtData('todos') // Invalidate todos in the background if the request succeeded.
}
})
2023-10-18 10:59:43 +00:00
< / script >
```
## Type
```ts
useNuxtData< DataT = any > (key: string): { data: Ref< DataT | null > }
2022-12-05 13:07:33 +00:00
```