0
0
Vueframework~20 mins

Watch and watchEffect in Vue - Practice Problems & Coding Challenges

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!
component_behavior
intermediate
2:00remaining
Difference in reactive triggers between watch and watchEffect

Consider a Vue 3 component using both watch and watchEffect. Which statement correctly describes when each watcher runs?

Vue
import { ref, watch, watchEffect } from 'vue';

const count = ref(0);
const double = ref(0);

watch(count, (newVal) => {
  double.value = newVal * 2;
});

watchEffect(() => {
  console.log(`Count is: ${count.value}`);
});
A<code>watch</code> runs immediately on setup; <code>watchEffect</code> runs only when dependencies change.
B<code>watch</code> runs whenever any reactive property changes; <code>watchEffect</code> runs only once on setup.
C<code>watch</code> runs only when <code>count</code> changes; <code>watchEffect</code> runs immediately and whenever dependencies change.
D<code>watch</code> and <code>watchEffect</code> both run only once on setup.
Attempts:
2 left
💡 Hint

Think about when each watcher first runs and how they track dependencies.

state_output
intermediate
2:00remaining
Output of watchEffect with multiple reactive sources

What will be logged to the console after the following code runs and count.value changes from 1 to 2?

Vue
import { ref, watchEffect } from 'vue';

const count = ref(1);
const message = ref('hello');

watchEffect(() => {
  console.log(`Count: ${count.value}, Message: ${message.value}`);
});

count.value = 2;
A
Count: 1, Message: hello
Count: 2, Message: hello
BCount: 1, Message: hello
CCount: 2, Message: hello
D
Count: 1, Message: hello
Count: 1, Message: hello
Attempts:
2 left
💡 Hint

Remember when watchEffect runs its function.

📝 Syntax
advanced
2:00remaining
Correct syntax to watch multiple sources with watch

Which option shows the correct way to watch two reactive refs a and b and react when either changes?

Vue
import { ref, watch } from 'vue';

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

Think about how to pass multiple sources as an array.

🔧 Debug
advanced
2:00remaining
Identify the error in this watchEffect usage

What error will this code produce when run in a Vue 3 component?

Vue
import { ref, watchEffect } from 'vue';

const count = ref(0);

watchEffect(() => {
  count.value++;
});
ATypeError: count.value is not a function
BSyntaxError: Unexpected token
CNo error, runs fine resetting count after 5
DMaximum call stack size exceeded (infinite loop error)
Attempts:
2 left
💡 Hint

Consider what happens when you change a reactive value inside watchEffect.

🧠 Conceptual
expert
3:00remaining
Why prefer watch over watchEffect for expensive side effects?

In Vue 3, why might you choose watch instead of watchEffect when performing expensive operations based on reactive data?

A<code>watch</code> runs only when specific sources change, avoiding unnecessary runs; <code>watchEffect</code> runs immediately and on any dependency change.
B<code>watchEffect</code> caches results automatically; <code>watch</code> does not.
C<code>watch</code> runs synchronously; <code>watchEffect</code> runs asynchronously.
D<code>watchEffect</code> cannot access reactive data; <code>watch</code> can.
Attempts:
2 left
💡 Hint

Think about control over when the watcher runs.