Vue automatically knows which parts of your app need to update when data changes. This makes your app fast and easy to write.
How Vue tracks dependencies automatically
import { ref, computed, watchEffect } from 'vue' const count = ref(0) const double = computed(() => count.value * 2) watchEffect(() => { console.log(`Count is ${count.value}`) })
ref() creates reactive data Vue can track.
computed() creates a value that updates automatically when its dependencies change.
greeting updates automatically when name changes.import { ref, computed } from 'vue' const name = ref('Alice') const greeting = computed(() => `Hello, ${name.value}!`)
watchEffect runs the function immediately and reruns it whenever count changes.import { ref, watchEffect } from 'vue' const count = ref(0) watchEffect(() => { console.log(`Count changed to ${count.value}`) })
This Vue component shows a count and its double. When you click the button, count increases, and double updates automatically because Vue tracks the dependency.
<template>
<div>
<p>Count: {{ count }}</p>
<p>Double: {{ double }}</p>
<button @click="increment">Add 1</button>
</div>
</template>
<script setup>
import { ref, computed } from 'vue'
const count = ref(0)
const double = computed(() => count.value * 2)
function increment() {
count.value++
}
</script>Vue uses a system called reactivity to track which data your code uses.
When reactive data changes, Vue knows exactly which parts of your app to update.
This automatic tracking means you write less code and get faster updates.
Vue tracks dependencies automatically using reactive data like ref and computed.
This makes your app update only what needs to change, keeping it fast.
You don't have to manually tell Vue what to watch; it figures it out for you.
Practice
Solution
Step 1: Understand Vue's reactive system
Vue uses special reactive data wrappers likerefandcomputedto track dependencies automatically.Step 2: How Vue tracks dependencies
When rendering, Vue records which reactive data is accessed, so it knows what to update when data changes.Final Answer:
Vue tracks reactive data dependencies during rendering usingrefandcomputed. -> Option DQuick Check:
Automatic dependency tracking = Vue tracks reactive data dependencies during rendering usingrefandcomputed. [OK]
- Thinking you must manually specify dependencies
- Believing Vue updates everything on any change
- Assuming Vue uses timers to detect changes
Solution
Step 1: Identify the correct reactive API
In Vue 3,refis used to create a reactive reference to a primitive value like a number.Step 2: Check other options
reactiveis for objects,computedcreates derived values, andwatchobserves changes but does not create reactive data.Final Answer:
const count = ref(0); -> Option BQuick Check:
Reactive primitive = ref() = const count = ref(0); [OK]
- Using reactive() for primitive values
- Confusing computed() with ref()
- Trying to use watch() to create reactive data
const count = ref(0); const double = computed(() => count.value * 2); console.log(double.value); count.value = 5; console.log(double.value);
What will be printed in the console?
Solution
Step 1: Initial value of count and double
Initially,count.valueis 0, sodouble.valueis 0 * 2 = 0.Step 2: After updating count
Whencount.valuechanges to 5,doubleautomatically recalculates to 5 * 2 = 10.Final Answer:
0 and 10 -> Option AQuick Check:
Computed updates with reactive changes = 0 and 10 [OK]
- Expecting double to stay 0 after count changes
- Confusing initial and updated values
- Not accessing .value on refs
const count = ref(0); const double = computed(() => count * 2); console.log(double.value);
Solution
Step 1: Check how ref is accessed inside computed
Refs store their value inside a.valueproperty, so you must usecount.valueto get the number.Step 2: Identify the error
The code usescountdirectly, which is a ref object, not the number, causing wrong behavior.Final Answer:
You must use count.value inside computed, not count directly. -> Option CQuick Check:
Access ref with .value inside computed = You must use count.value inside computed, not count directly. [OK]
- Using ref variable directly without .value
- Thinking computed can't use ref
- Calling computed like a function instead of .value
Solution
Step 1: Understand reactive data and computed
A reactive array wrapped withreforreactivecan be used inside acomputedproperty to automatically track dependencies.Step 2: Why computed is best here
The computed property recalculates the count whenever the items array changes, so the UI stays updated without manual intervention.Final Answer:
Use acomputedproperty that filters the reactive items array and returns the count of items with price > 10. -> Option AQuick Check:
Computed auto-updates with reactive data = Use acomputedproperty that filters the reactive items array and returns the count of items with price > 10. [OK]
- Manually watching and updating counts instead of computed
- Using methods that don't auto-update
- Trying to update counts inside template variables
Ready to prove it?
Pass the test and this pattern moves unknown
