0 day streak
javascript / beginner
Snippet

Watching Reactive Changes

The watch function allows you to perform 'side effects' (like fetching data) whenever a specific piece of data, such as a prop, changes its value.

snippet.js
javascript
1
2
3
4
5
import { watch } from 'vue';
watch(() => props.userId, (newId) => {
console.log('User ID changed to:', newId);
});
vue
Breakdown
1
watch(() => props.userId, ...)
Defines the source to watch; using a function here ensures we track the reactive prop correctly.
2
(newId) => { ... }
This function runs automatically and receives the updated value as its first argument.