0
0
Vueframework~20 mins

Watch vs computed decision in Vue - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Reactivity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
When to use computed vs watch in Vue?

In Vue 3, both computed and watch can react to reactive data changes. Which statement best describes when to use computed instead of watch?

AUse <code>computed</code> when you want to derive a value based on reactive data and cache it automatically.
BUse <code>computed</code> when you want to perform side effects like API calls on data change.
CUse <code>computed</code> when you want to watch multiple unrelated reactive sources separately.
DUse <code>computed</code> only for asynchronous operations triggered by data changes.
Attempts:
2 left
💡 Hint

Think about whether you want a value that updates automatically and is cached, or if you want to run code when data changes.

component_behavior
intermediate
1:30remaining
What is the output of this Vue computed property?

Given the following Vue 3 computed property, what will be the value of fullName.value after firstName.value = 'Jane'?

Vue
import { ref, computed } from 'vue';
const firstName = ref('John');
const lastName = ref('Doe');
const fullName = computed(() => `${firstName.value} ${lastName.value}`);

firstName.value = 'Jane';

// What is fullName.value now?
A'John Doe'
B'Jane Doe'
Cundefined
DThrows an error because computed is read-only
Attempts:
2 left
💡 Hint

Remember that computed properties update automatically when their dependencies change.

lifecycle
advanced
2:00remaining
What happens when a watch callback updates the watched reactive source?

Consider this Vue 3 code snippet:

import { ref, watch } from 'vue';
const count = ref(0);
watch(count, (newVal, oldVal) => {
  if (newVal < 3) {
    count.value++;
  }
});
count.value = 0;

What will be the final value of count.value after this code runs?

A3
B1
C0
DInfinite loop causing stack overflow
Attempts:
2 left
💡 Hint

Think about how watch triggers when the reactive source changes inside its own callback.

📝 Syntax
advanced
1:30remaining
Which watch syntax correctly watches multiple reactive sources?

Which option correctly watches two reactive refs a and b and runs a callback when either changes?

Awatch(a, b, (newVal, oldVal) => { console.log(newVal, oldVal); });
Bwatch(a && b, (newVal, oldVal) => { console.log(newVal); });
Cwatch([a, b], ([newA, newB], [oldA, oldB]) => { console.log(newA, newB); });
Dwatch({a, b}, (newVal, oldVal) => { console.log(newVal); });
Attempts:
2 left
💡 Hint

Check the Vue 3 docs for watching multiple sources syntax.

🔧 Debug
expert
2:30remaining
Why does this computed property not update when obj.count changes?

Given this Vue 3 code:

import { reactive, computed } from 'vue';
const obj = reactive({ count: 0 });
const doubleCount = computed(() => obj.count * 2);

obj.count = 1;

// doubleCount.value is still 0. Why?
ABecause the code is missing a return statement inside the computed function.
BBecause the assignment <code>obj.count = 1</code> is not reactive and does not trigger computed updates.
CBecause the computed property was created before <code>obj.count</code> was reactive, so it never tracked it.
DBecause <code>obj</code> is reactive but <code>count</code> is not a ref, so computed does not track it.
Attempts:
2 left
💡 Hint

Check the computed function syntax carefully.