0
0
Vueframework~10 mins

Trigger and track for manual reactivity in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Trigger and track for manual reactivity
Create reactive object
Call track() to register dependency
Use reactive data in effect
Call trigger() to notify changes
Effect re-runs with updated data
This flow shows how manual reactivity works by tracking dependencies and triggering updates when data changes.
Execution Sample
Vue
import { reactive, effect, track, trigger } from 'vue';
const state = reactive({ count: 0 });
effect(() => {
  track(state, 'count');
  console.log(state.count);
});
trigger(state, 'count');
This code manually tracks the 'count' property and triggers an effect to re-run when 'count' changes.
Execution Table
StepActionEvaluationResult
1Create reactive object 'state' with count=0state.count = 0state.count = 0
2Run effect functionCalls track(state, 'count')Dependency on state.count registered
3Inside effect, console.log(state.count)state.count = 0Output: 0
4Call trigger(state, 'count')Notify dependenciesEffect re-runs
5Effect re-runs, calls track(state, 'count')Dependency re-registeredDependency on state.count registered
6Inside effect, console.log(state.count)state.count = 0Output: 0
💡 No further triggers, effect stops re-running
Variable Tracker
VariableStartAfter Step 1After Step 4Final
state.countundefined000
Key Moments - 2 Insights
Why do we need to call track() manually inside the effect?
Because manual reactivity requires explicitly registering which properties the effect depends on, as shown in steps 2 and 5 of the execution_table.
What happens when trigger() is called?
Trigger notifies all effects that depend on the changed property to re-run, as seen in step 4 where the effect re-runs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is output when the effect first runs?
Aundefined
B0
C1
DError
💡 Hint
Check step 3 in the execution_table where console.log outputs the value.
At which step does the effect re-run after trigger() is called?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look at the execution_table steps after trigger() call at step 4.
If track() was not called inside the effect, what would happen when trigger() is called?
AError would occur
BEffect would still re-run
CEffect would not re-run
DValue would change automatically
💡 Hint
Refer to the key_moments explanation about manual dependency registration.
Concept Snapshot
Manual reactivity in Vue:
- Use track(obj, key) inside effect to register dependencies
- Use trigger(obj, key) to notify changes
- Effects re-run only when triggered
- Requires explicit calls unlike automatic reactivity
Full Transcript
This visual execution shows how manual reactivity works in Vue. First, a reactive object 'state' is created with a count property set to 0. An effect function runs and calls track(state, 'count') to register that it depends on 'count'. The effect logs the current count value, which is 0. When trigger(state, 'count') is called, it notifies the effect to re-run. The effect runs again, re-registers the dependency, and logs the count value again. Without calling track(), the effect would not know to re-run when triggered. This manual approach requires explicit tracking and triggering to update reactive effects.