0
0
Vueframework~10 mins

Typing ref and reactive in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Typing ref and reactive
Import ref, reactive
Declare ref with type
Declare reactive with type
Use variables in template or logic
Update values
Reactivity triggers updates
This flow shows how to import and declare typed ref and reactive variables, use them, update their values, and trigger reactive updates.
Execution Sample
Vue
import { ref, reactive } from 'vue'

const count = ref<number>(0)
const state = reactive<{ name: string, age: number }>({ name: 'Alice', age: 30 })

count.value++
state.age = 31
This code creates a typed ref and a typed reactive object, then updates their values.
Execution Table
StepActionVariableValue BeforeValue AfterReactivity Triggered
1Declare count as ref<number>(0)count.valueundefined0No
2Declare state as reactive<{name:string, age:number}>stateundefined{ name: 'Alice', age: 30 }No
3Increment count.valuecount.value01Yes
4Update state.age to 31state.age3031Yes
💡 All declarations and updates done, reactive system ready to update UI or watchers.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
count.valueundefined00111
state.nameundefinedundefinedAliceAliceAliceAlice
state.ageundefinedundefined30303131
Key Moments - 3 Insights
Why do we use count.value to access the ref value?
Because ref wraps the value inside an object with a .value property, as shown in steps 1 and 3 of the execution_table.
How does typing reactive differ from typing ref?
Typing reactive applies to the whole object shape, while typing ref applies to the single value inside .value, as seen in steps 1 and 2.
Does updating count.value or state.age trigger reactivity?
Yes, updating these triggers reactivity, shown in steps 3 and 4 where Reactivity Triggered is 'Yes'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is count.value after step 3?
A0
B1
Cundefined
D2
💡 Hint
Check the 'Value After' column for count.value at step 3.
At which step does state.age change from 30 to 31?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Action' and 'Value After' columns for state.age.
If we remove typing from ref, what changes in the execution_table?
ANo change in values, but type safety is lost
Bcount.value becomes undefined
CReactivity stops working
Dstate object loses properties
💡 Hint
Typing affects development safety, not runtime values shown in execution_table.
Concept Snapshot
Typing ref and reactive in Vue:
- Use ref<Type>(initial) for single reactive values
- Use reactive<Type>(object) for reactive objects
- Access ref value with .value
- Updates trigger reactivity and UI updates
- Typing helps catch errors but does not affect runtime behavior
Full Transcript
This lesson shows how to use typed ref and reactive in Vue. First, we import ref and reactive. Then we declare a ref with a number type and a reactive object with a typed shape. We update the ref's value using .value and update a property inside the reactive object. Each update triggers Vue's reactivity system to update the UI or watchers. The execution table tracks each step's variable values and whether reactivity triggers. Key points include accessing ref values with .value and how typing helps with code safety. The visual quiz tests understanding of variable values and typing effects.