useFetch?Consider this Nuxt 3 component that fetches user data:
const { data, pending, error } = useFetch('/api/user')
return () => {
if (pending.value) return 'Loading...'
if (error.value) return 'Error!'
return `User: ${data.value.name}`
}Assuming the API returns { name: 'Alice' }, what will the rendered output be?
const { data, pending, error } = useFetch('/api/user') return () => { if (pending.value) return 'Loading...' if (error.value) return 'Error!' return `User: ${data.value.name}` }
Think about what useFetch returns and when the component renders.
useFetch returns reactive references. Once the data is fetched successfully, pending becomes false and data.value holds the API response. So the component renders User: Alice.
data after useAsyncData resolves?Given this Nuxt 3 setup:
const { data, error } = await useAsyncData('post', () => $fetch('/api/post/1'))If the API returns { id: 1, title: 'Hello' }, what is data.value?
const { data, error } = await useAsyncData('post', () => $fetch('/api/post/1'))
useAsyncData resolves with the fetched data inside data.value.
After awaiting useAsyncData, data.value contains the API response object.
useFetch with error handling in Nuxt 3?Choose the code snippet that correctly fetches data and handles errors using useFetch in a Nuxt 3 setup.
Remember that error is a ref and must be accessed with .value.
useFetch returns refs for data and error. To check error, use error.value. Option A correctly checks and logs the error.
useAsyncData call cause a runtime error?Examine this code snippet:
const { data } = useAsyncData(() => fetch('/api/items').then(res => res.json()))
console.log(data.value.length)What causes the runtime error?
const { data } = useAsyncData(() => fetch('/api/items').then(res => res.json())) console.log(data.value.length)
Think about when data.value is available after calling useAsyncData.
useAsyncData returns a ref that is initially undefined until the promise resolves. Accessing data.value.length immediately causes an error because data.value is undefined at that moment.
useFetch vs useAsyncData in Nuxt 3 is correct?Choose the correct statement about the differences between useFetch and useAsyncData in Nuxt 3.
Think about caching and SSR support differences.
useAsyncData caches data by a key and is designed for SSR and hydration. useFetch is a simpler wrapper around fetch with reactive refs but does not cache by default.