Advanced reactivity helps your app update only what needs to change. This makes your app faster and smoother.
Why advanced reactivity matters in Vue
import { reactive, ref, computed, watch } from 'vue'; const state = reactive({ count: 0 }); const doubled = computed(() => state.count * 2); watch(() => state.count, (newVal, oldVal) => { console.log(`Count changed from ${oldVal} to ${newVal}`); });
reactive makes an object reactive so Vue tracks its changes.
computed creates a value that updates automatically when dependencies change.
ref for a simple reactive number.const count = ref(0); function increment() { count.value++; }
reactive for an object so Vue tracks changes inside it.const state = reactive({ name: 'Alice', age: 30 }); state.age = 31;
computed automatically updates when count changes.const doubled = computed(() => count.value * 2);
watch runs code when a reactive value changes.watch(() => state.age, (newAge, oldAge) => {
console.log(`Age changed from ${oldAge} to ${newAge}`);
});This Vue component shows a count and its doubled value. When you click the button, the count increases and the doubled value updates automatically thanks to reactivity.
<template>
<div>
<p>Count: {{ count }}</p>
<p>Doubled: {{ doubled }}</p>
<button @click="increment">Add 1</button>
</div>
</template>
<script setup>
import { ref, computed } from 'vue';
const count = ref(0);
const doubled = computed(() => count.value * 2);
function increment() {
count.value++;
}
</script>Advanced reactivity helps avoid updating the whole page, only the parts that changed.
Using ref and reactive properly keeps your code clean and efficient.
Watchers let you react to changes for side effects like logging or fetching data.
Advanced reactivity keeps your app fast by updating only what changes.
Use ref, reactive, computed, and watch to manage reactive data.
This makes your app easier to build and maintain.
Practice
Solution
Step 1: Understand reactivity basics
Reactivity means Vue tracks data changes and updates the UI accordingly.Step 2: Recognize advanced reactivity benefits
Advanced reactivity updates only changed parts, improving speed and efficiency.Final Answer:
It updates only the parts of the app that change, making it faster. -> Option BQuick Check:
Advanced reactivity = selective updates = It updates only the parts of the app that change, making it faster. [OK]
- Thinking the whole app reloads on every change
- Believing reactivity disables updates
- Assuming manual DOM updates are needed
Solution
Step 1: Identify Vue 3 reactive APIs
Vue 3 usesrefto create reactive primitive values like numbers.Step 2: Match correct usage
reactiveis for objects,computedfor derived values,watchfor side effects.Final Answer:
const count = ref(0); -> Option AQuick Check:
Use ref() for reactive primitives = const count = ref(0); [OK]
- Using reactive() for primitive values
- Confusing computed() with ref()
- Trying to watch() to create reactive data
import { ref, watch } from 'vue';
const count = ref(0);
watch(count, (newVal) => {
console.log('Count changed to', newVal);
});
count.value = 5;
count.value = 10;Solution
Step 1: Understand watch behavior
The watcher runs only when the reactive value changes, not on initial setup.Step 2: Trace value changes
Setting count.value to 5 triggers the watcher logging 'Count changed to 5'. Then setting to 10 triggers 'Count changed to 10'.Final Answer:
Count changed to 5 Count changed to 10 -> Option CQuick Check:
Watch triggers on changes, not initial = Count changed to 5 Count changed to 10 [OK]
- Expecting watch to run immediately on setup
- Thinking initial value triggers watch
- Missing that watch reacts only to changes
import { reactive } from 'vue';
const state = reactive({ count: 0 });
function increment() {
state.count++;
}
increment();
console.log(state.count);Solution
Step 1: Review reactive object usage
Usingreactivecreates a reactive object whose properties can be accessed and mutated directly.Step 2: Check increment function
Incrementingstate.countdirectly is valid and updates the reactive state.Final Answer:
There is no error; the code works correctly. -> Option DQuick Check:
reactive objects allow direct property mutation = There is no error; the code works correctly. [OK]
- Confusing reactive() with ref() usage
- Trying to use .value on reactive object properties
- Thinking reactive properties are immutable
import { ref, computed } from 'vue';
const count = ref(3);
const doubleCount = ???
console.log(doubleCount.value);Solution
Step 1: Understand computed properties
Computed properties use a function returning a value based on reactive data and update automatically.Step 2: Identify correct computed syntax
The correct syntax iscomputed(() => count.value * 2)to create a reactive doubleCount.Final Answer:
const doubleCount = computed(() => count.value * 2); -> Option AQuick Check:
Use computed with a function returning reactive value = const doubleCount = computed(() => count.value * 2); [OK]
- Passing expression directly to computed without a function
- Using ref() instead of computed() for derived values
- Trying to use reactive() for computed properties
Ready to prove it?
Pass the test and this pattern moves unknown
