0
0
Vueframework~10 mins

Nuxt data fetching (useFetch, useAsyncData) in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nuxt data fetching (useFetch, useAsyncData)
Component Setup
Call useFetch or useAsyncData
Start Data Fetching
Wait for Promise
Success
Store Data in Reactive State
Component Renders with Data
Error
Store Error State
Component Renders with Error
The component calls useFetch or useAsyncData to start fetching data asynchronously, waits for the result, then stores the data or error in reactive state for rendering.
Execution Sample
Vue
const { data, pending, error } = useFetch('/api/items')
This code fetches data from '/api/items' and tracks loading and error states.
Execution Table
StepActionState BeforeResultState AfterComponent Render
1Component setup startsdata: undefined, pending: undefined, error: undefinedCall useFetchpending: true, data: undefined, error: undefinedRender loading state (pending true)
2Fetch promise startspending: trueFetching data from /api/itemspending: trueStill loading
3Fetch promise resolvespending: trueData received: [{id:1,name:'A'},{id:2,name:'B'}]pending: false, data: [{id:1,name:'A'},{id:2,name:'B'}], error: nullRender data list
4User sees data rendereddata presentDisplay list itemsNo state changeData visible on screen
5If fetch failedpending: trueError receivedpending: false, error: Error object, data: nullRender error message
6Component unmountsAny stateCleanupState clearedComponent removed
ExitNo more updatesFinal state stableNo actionStable stateComponent shows final UI
💡 Data fetch completes successfully or with error, component renders accordingly and stops updating.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
dataundefinedundefined[{id:1,name:'A'},{id:2,name:'B'}]null[{id:1,name:'A'},{id:2,name:'B'}] or null
pendingundefinedtruefalsefalsefalse
errorundefinedundefinednullError objectnull or Error object
Key Moments - 3 Insights
Why is 'pending' true before data arrives?
'pending' is true during the fetch promise to indicate loading, as shown in execution_table step 1 and 2.
What happens if the fetch fails?
If fetch fails, 'error' stores the error object and 'data' becomes null, shown in step 5 of execution_table.
Why does the component re-render after data arrives?
Because 'data' changes from undefined to the fetched array, triggering reactive updates and re-render (step 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'pending' at step 3?
Afalse
Btrue
Cundefined
Dnull
💡 Hint
Check the 'State After' column at step 3 in execution_table.
At which step does the component render the fetched data?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look for 'Render data list' or 'Data visible on screen' in execution_table.
If the fetch fails, what happens to the 'data' variable?
AIt becomes an empty array
BIt becomes null
CIt stays undefined
DIt keeps previous data
💡 Hint
See execution_table step 5 and variable_tracker for 'data' changes on error.
Concept Snapshot
Nuxt data fetching uses useFetch or useAsyncData.
Call them in setup to start async fetch.
They return reactive data, pending, and error states.
Component renders loading while pending is true.
On success, data updates and component re-renders.
On error, error state updates and shows error UI.
Full Transcript
In Nuxt, you fetch data inside components using useFetch or useAsyncData. When the component starts, these functions begin fetching data asynchronously. While waiting, the 'pending' state is true, so the component can show a loading message. When the data arrives successfully, 'pending' becomes false and 'data' holds the fetched result. The component then re-renders to show the data. If the fetch fails, 'error' holds the error details and the component shows an error message. This flow ensures the UI updates automatically based on fetch status.