Concept Flow - Watch vs computed decision
Start
Define reactive data
Computed property
Auto recalculates
Used in template
Update UI
Shows how reactive data triggers either computed properties or watch effects, leading to UI updates.
const count = ref(1) const double = computed(() => count.value * 2) watch(count, (newVal) => console.log('Count changed:', newVal)) count.value = 2
| Step | Action | count.value | double.value | Watch Triggered | Output |
|---|---|---|---|---|---|
| 1 | Initialize count=1 | 1 | 2 | No | |
| 2 | Read double.value | 1 | 2 | No | |
| 3 | Change count.value = 2 | 2 | 2 | Yes | Count changed: 2 |
| 4 | Read double.value after change | 2 | 4 | No |
| Variable | Start | After Step 3 | Final |
|---|---|---|---|
| count.value | 1 | 2 | 2 |
| double.value | 2 | 2 | 4 |
| Watch Triggered | No | Yes | No |
Vue reactive data triggers updates. Computed properties auto-recalculate when dependencies change. Watch runs code on reactive data changes for side effects. Use computed for derived data, watch for reacting to changes. Computed is cached; watch runs every change. Choose based on need: value or side effect.