Bird
Raised Fist0
Vueframework~20 mins

Watch and watchEffect in Vue - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between watch and watchEffect in Vue?
easy
A. watchEffect only runs once, watch runs continuously.
B. watch tracks specific reactive sources, watchEffect tracks all reactive dependencies automatically.
C. watch runs immediately on setup, watchEffect waits for changes.
D. watchEffect requires manual cleanup, watch does not.

Solution

  1. Step 1: Understand watch behavior

    watch listens to specific reactive data sources you specify and runs a callback only when those change.
  2. Step 2: Understand watchEffect behavior

    watchEffect runs immediately and tracks all reactive data accessed inside it, rerunning whenever any of those change.
  3. Final Answer:

    watch tracks specific reactive sources, watchEffect tracks all reactive dependencies automatically. -> Option B
  4. Quick Check:

    watch vs watchEffect difference = watch tracks specific reactive sources, watchEffect tracks all reactive dependencies automatically. [OK]
Hint: Remember: watch tracks specific data, watchEffect tracks all used data [OK]
Common Mistakes:
  • Thinking watchEffect only runs once
  • Assuming watch runs immediately on setup
  • Confusing cleanup requirements
2. Which of the following is the correct syntax to watch a reactive variable count in Vue 3 Composition API?
easy
A. watch(count, (newVal, oldVal) => { console.log(newVal) })
B. watch(count, () => { console.log(count) })
C. watchEffect(() => { console.log(count) })
D. watch(() => count, (newVal, oldVal) => { console.log(newVal) })

Solution

  1. Step 1: Identify correct watch syntax

    In Vue 3 Composition API, watch accepts a ref directly as the first argument, which automatically watches its .value.
  2. Step 2: Check callback parameters

    The callback receives newVal and oldVal as parameters, correctly used in watch(count, (newVal, oldVal) => { console.log(newVal) }) to log the new value.
  3. Final Answer:

    watch(count, (newVal, oldVal) => { console.log(newVal) }) -> Option A
  4. Quick Check:

    Correct watch syntax uses ref directly = watch(count, (newVal, oldVal) => { console.log(newVal) }) [OK]
Hint: Pass ref directly to watch [OK]
Common Mistakes:
  • Using getter returning ref object like () => count
  • Confusing watch with watchEffect syntax
  • Omitting callback parameters
3. Given the code below, what will be logged to the console after count.value = 2 is executed?
import { ref, watch } from 'vue'
const count = ref(1)
watch(() => count.value, (newVal, oldVal) => {
  console.log(`Changed from ${oldVal} to ${newVal}`)
})
count.value = 2
medium
A. SyntaxError
B. Changed from undefined to 2
C. No output
D. Changed from 1 to 2

Solution

  1. Step 1: Understand watch callback parameters

    The watch callback receives old and new values of the watched reactive source.
  2. Step 2: Trace the change

    Initially, count.value is 1. When changed to 2, the callback logs the change from 1 to 2.
  3. Final Answer:

    Changed from 1 to 2 -> Option D
  4. Quick Check:

    watch logs old and new values correctly = Changed from 1 to 2 [OK]
Hint: watch callback shows old and new values on change [OK]
Common Mistakes:
  • Expecting no output on change
  • Confusing initial value with undefined
  • Misunderstanding callback parameters
4. What is wrong with this Vue 3 code using watch? How to fix it?
import { ref, watch } from 'vue'
const count = ref(0)
watch(count, (newVal) => {
  console.log(newVal)
})
medium
A. No error; code works fine
B. Missing cleanup function; fix by adding cleanup inside callback
C. Callback missing oldVal parameter; fix by adding it
D. Passing reactive variable directly; fix by passing a getter function like () => count

Solution

  1. Step 1: Identify watch argument type

    In Vue 3, watch(count, callback) is valid; it automatically tracks count.value changes.
  2. Step 2: Callback parameters

    The callback receives newVal and optional oldVal; omitting oldVal is fine.
  3. Final Answer:

    No error; code works fine -> Option A
  4. Quick Check:

    watch accepts ref directly = No error; code works fine [OK]
Hint: watch accepts refs directly [OK]
Common Mistakes:
  • Thinking ref must be wrapped in getter
  • Assuming oldVal always required
  • Believing code has error
5. You want to run a side effect immediately and re-run it whenever any reactive data inside changes, but also want to stop the effect manually later. Which Vue 3 API and pattern should you use?
hard
A. Use watch without immediate option and no cleanup
B. Use watch with immediate: true option and no stop function
C. Use watchEffect and store the returned stop function to call later
D. Use watchEffect but it cannot be stopped manually

Solution

  1. Step 1: Identify immediate reactive effect

    watchEffect runs immediately and tracks all reactive dependencies automatically.
  2. Step 2: Understand stopping effect

    watchEffect returns a stop function that can be called to stop the effect manually.
  3. Final Answer:

    Use watchEffect and store the returned stop function to call later -> Option C
  4. Quick Check:

    watchEffect runs immediately and can be stopped = Use watchEffect and store the returned stop function to call later [OK]
Hint: watchEffect returns stop function to control effect lifecycle [OK]
Common Mistakes:
  • Thinking watchEffect cannot be stopped
  • Using watch without immediate for immediate run
  • Ignoring stop function usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard