0
0
Vueframework~20 mins

Watchers for side effects in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Watcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the watched property changes?
Consider this Vue 3 component using the Composition API. What will be logged to the console when the count value changes from 0 to 1?
Vue
import { ref, watch } from 'vue';

export default {
  setup() {
    const count = ref(0);
    watch(count, (newVal, oldVal) => {
      console.log(`Count changed from ${oldVal} to ${newVal}`);
    });
    return { count };
  }
};
ALogs: 'Count changed from 0 to 1'
BLogs: 'Count changed from 1 to 0'
CNo log output because watch is not triggered
DThrows a runtime error because watch is used incorrectly
Attempts:
2 left
💡 Hint
Remember that the watcher callback receives the new and old values in that order.
state_output
intermediate
2:00remaining
What is the value of message after the watcher runs?
In this Vue 3 component, what will be the value of message after name changes from 'Alice' to 'Bob'?
Vue
import { ref, watch } from 'vue';

export default {
  setup() {
    const name = ref('Alice');
    const message = ref('');
    watch(name, (newName) => {
      message.value = `Hello, ${newName}!`;
    });
    return { name, message };
  }
};
A"Hello, !"
B"Hello, Alice!"
C"Hello, Bob!"
DAn empty string ""
Attempts:
2 left
💡 Hint
The watcher updates message whenever name changes.
📝 Syntax
advanced
2:00remaining
Which watcher syntax correctly watches multiple sources?
You want to watch two reactive refs a and b and run a callback when either changes. Which option uses the correct Vue 3 watcher syntax?
Vue
import { ref, watch } from 'vue';

const a = ref(1);
const b = ref(2);

// Which watcher is correct?
Awatch(a && b, (newVal, oldVal) => { console.log(newVal); });
Bwatch([a, b], ([newA, newB], [oldA, oldB]) => { console.log(newA, newB); });
Cwatch(a, b, (newA, newB) => { console.log(newA, newB); });
Dwatch({a, b}, (newVals, oldVals) => { console.log(newVals); });
Attempts:
2 left
💡 Hint
To watch multiple refs, pass an array of sources as the first argument.
🔧 Debug
advanced
2:00remaining
Why does this watcher not trigger on nested object property change?
Given this Vue 3 component, why does the watcher not run when user.name changes?
Vue
import { reactive, watch } from 'vue';

export default {
  setup() {
    const user = reactive({ name: 'Alice', age: 30 });
    watch(user, (newUser) => {
      console.log('User changed:', newUser);
    }, { deep: true });
    // Later in code:
    user.name = 'Bob';
    return { user };
  }
};
AThe user object is not reactive because ref() was not used.
BReactive objects cannot be watched in Vue 3.
CThe watcher callback is missing the old value parameter.
DThe watcher does not deep watch by default, so nested changes are ignored.
Attempts:
2 left
💡 Hint
By default, watchers do not track nested property changes unless specified.
🧠 Conceptual
expert
2:00remaining
What is the effect of immediate option in watchers?
In Vue 3, what happens when you add immediate: true to a watcher?
Vue
import { ref, watch } from 'vue';

const count = ref(0);
watch(count, (newVal, oldVal) => {
  console.log('Count changed:', newVal);
}, { immediate: true });
AThe watcher callback runs immediately with the current value, then on every change.
BThe watcher callback runs twice on every change.
CThe watcher callback runs only on changes, ignoring the initial value.
DThe watcher callback runs only once and never again.
Attempts:
2 left
💡 Hint
Immediate watchers run the callback once right after setup.