0
0
Vueframework~10 mins

Effective scope for cleanup in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Effective scope for cleanup
Component Setup
Create Reactive State
Register Effect or Watch
Effect Runs
Component Unmount
Cleanup Function Runs
Resources Freed
Component Removed or Updated
This flow shows how Vue sets up reactive effects and cleans them up automatically when the component unmounts.
Execution Sample
Vue
import { ref, watchEffect, onUnmounted } from 'vue'

const count = ref(0)
const stop = watchEffect(() => {
  console.log(`Count is: ${count.value}`)
})
onUnmounted(() => stop())
This code sets up a reactive effect that logs count changes and cleans up the watcher when the component unmounts.
Execution Table
StepActionReactive StateEffect StatusCleanup StatusOutput
1Initialize count = 0count=0No effect yetNo cleanup
2Start watchEffectcount=0Effect activeNo cleanupLogs: Count is: 0
3Update count to 1count=1Effect re-runsNo cleanupLogs: Count is: 1
4Component unmount triggers cleanupcount=1Effect stoppedCleanup function called
5Effect stopped, no further logscount=1No effectCleanup done
💡 Cleanup runs when component unmounts, stopping the effect and freeing resources.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
count00111
Effect activefalsetruetruefalsefalse
Cleanup calledfalsefalsefalsetruetrue
Key Moments - 2 Insights
Why does the cleanup function run only when the component unmounts?
Because Vue calls the cleanup registered by onUnmounted only when the component is removed, as shown in step 4 of the execution_table.
What happens if we don't call the cleanup function returned by watchEffect?
Vue automatically stops the effect on component unmount, preventing memory leaks or unwanted side effects. Manual cleanup provides explicit control.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count after step 3?
A1
B0
Cundefined
Dnull
💡 Hint
Check the 'Reactive State' column at step 3 in the execution_table.
At which step does the cleanup function run?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Cleanup Status' column in the execution_table.
If we remove the onUnmounted cleanup call, what happens to the effect after unmount?
AEffect runs only once
BEffect stops automatically
CEffect keeps running causing potential leaks
DEffect throws an error
💡 Hint
Vue automatically cleans up reactive effects created in setup() on component unmount.
Concept Snapshot
Vue cleanup scope:
- Setup reactive effect with watchEffect
- Effect runs immediately and on dependency changes
- Vue automatically cleans up effects on component unmount
- Manual cleanup: onUnmounted(() => stop())
- Prevents memory leaks and stale effects
Full Transcript
In Vue, when you create reactive effects using watchEffect, the effect runs immediately and whenever its dependencies change. Vue automatically cleans up these effects when the component unmounts. Vue provides onUnmounted to register cleanup functions. The cleanup function returned by watchEffect stops the effect from running further. This process ensures resources are freed and the component behaves correctly. The execution table shows the effect starting, running on state change, and cleaning up on unmount.