0
0
Vueframework~10 mins

Reactive data with ref in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reactive data with ref
Create ref with initial value
Access ref.value to read
Change ref.value to update
Vue tracks ref change
Trigger reactive updates
Component re-renders with new value
This flow shows how Vue's ref creates reactive data, tracks changes, and triggers updates in the component.
Execution Sample
Vue
import { ref } from 'vue';
const count = ref(0);
console.log(count.value);
count.value = 5;
console.log(count.value);
This code creates a reactive count variable, reads its value, updates it, and reads the new value.
Execution Table
StepActionref.value BeforeOperationref.value AfterOutput
1Create ref with 0N/Acount = ref(0)0No output
2Read ref.value0console.log(count.value)00
3Update ref.value0count.value = 55No output
4Read updated ref.value5console.log(count.value)55
5End of code5No further operations5Execution stops
💡 Code ends after reading updated ref.value; reactive value is 5
Variable Tracker
VariableStartAfter Step 1After Step 3Final
count.valueundefined055
Key Moments - 2 Insights
Why do we use count.value instead of just count?
Because ref returns an object with a .value property holding the reactive data. The execution_table shows reading and updating count.value explicitly at steps 2 and 3.
Does changing count.value automatically update the UI?
Yes, Vue tracks changes to ref.value and triggers reactive updates. The flow diagram shows this reactive update after changing ref.value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the value of count.value after the update?
A0
B5
Cundefined
Dnull
💡 Hint
Check the 'ref.value After' column in step 3 of the execution_table.
At which step does the console output the initial value of count.value?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table for the first console.log.
If we changed count.value = 10 at step 3 instead of 5, what would be the output at step 4?
A10
B0
C5
Dundefined
💡 Hint
Refer to the variable_tracker and execution_table showing how ref.value updates affect output.
Concept Snapshot
Vue ref creates reactive data with ref(initialValue).
Access or update the value with .value property.
Changing .value triggers reactive updates.
Use ref for primitive reactive variables.
Always read/write with .value in setup or script.
Full Transcript
This lesson shows how Vue's ref function creates reactive data. We start by creating a ref with an initial value of 0. We read the value using count.value, which logs 0. Then we update count.value to 5. Vue tracks this change and triggers reactive updates. Reading count.value again logs 5. The key is that ref returns an object with a .value property holding the reactive data. Changing .value updates the reactive system and UI. This simple flow helps beginners understand how to use ref for reactive variables in Vue.