0
0
Vueframework~10 mins

Loading states pattern in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Loading states pattern
Component mounts
Set loading = true
Fetch data async
Data received?
NoWait
Yes
Set loading = false
Render data or loading indicator
This flow shows how a Vue component sets a loading flag, fetches data asynchronously, then updates the UI when data arrives.
Execution Sample
Vue
import { ref, onMounted } from 'vue';

const loading = ref(true);
const data = ref(null);

onMounted(async () => {
  data.value = await fetchData();
  loading.value = false;
});
This Vue code sets loading true, fetches data on mount, then sets loading false to show data.
Execution Table
StepActionloading.valuedata.valueUI Rendered
1Component mounts, loading set truetruenullShow loading spinner
2Start fetchData async calltruenullShow loading spinner
3fetchData resolves with datatrue{...data...}Show loading spinner
4Set loading to falsefalse{...data...}Show data content
5Render completes with datafalse{...data...}Show data content
💡 Loading is false and data is loaded, so UI shows data instead of spinner.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
loading.valuetruetruetruefalsefalse
data.valuenullnull{...data...}{...data...}{...data...}
Key Moments - 3 Insights
Why does the UI show a spinner before data arrives?
Because loading.value is true at steps 1-3, the UI renders the loading spinner as shown in the execution_table rows 1-3.
When does the UI switch from spinner to data?
At step 4, loading.value changes to false, triggering the UI to render the data content instead of the spinner.
What happens if fetchData takes a long time?
The loading.value stays true, so the spinner remains visible until data arrives, preventing empty or broken UI.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is loading.value at Step 3?
Atrue
Bfalse
Cnull
Dundefined
💡 Hint
Check the loading.value column at Step 3 in the execution_table.
At which step does the UI start showing the actual data instead of the spinner?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the UI Rendered column in the execution_table to find when data content appears.
If loading.value was never set to false, what would the UI show?
AData content
BEmpty screen
CLoading spinner forever
DError message
💡 Hint
Refer to the key_moments explanation about loading.value controlling the spinner visibility.
Concept Snapshot
Loading states pattern in Vue:
- Use a reactive loading flag (ref) set true initially
- Fetch data asynchronously onMounted
- When data arrives, set loading false
- UI shows spinner if loading true, else shows data
- Keeps UI clear and responsive during async fetch
Full Transcript
This visual execution shows how a Vue component uses a loading state pattern. When the component mounts, it sets a loading flag to true and starts fetching data asynchronously. While waiting, the UI shows a loading spinner. Once the data arrives, the loading flag is set to false, and the UI updates to show the fetched data. This pattern helps keep the user informed and prevents showing empty or broken content during data loading.