Bird
Raised Fist0
Vueframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using track and trigger in Vue's manual reactivity system?
easy
A. To handle HTTP requests inside Vue components.
B. To automatically update the UI without any developer intervention.
C. To create new Vue components dynamically.
D. To manually control when Vue updates the UI by tracking dependencies and triggering updates.

Solution

  1. Step 1: Understand manual reactivity concept

    Manual reactivity allows developers to decide when Vue should update the UI instead of automatic updates.
  2. Step 2: Role of track and trigger

    track watches properties for changes, and trigger notifies Vue to update dependents when changes occur.
  3. Final Answer:

    To manually control when Vue updates the UI by tracking dependencies and triggering updates. -> Option D
  4. Quick Check:

    Manual reactivity = control updates [OK]
Hint: Remember: track watches, trigger updates UI manually [OK]
Common Mistakes:
  • Thinking Vue updates UI automatically without manual triggers
  • Confusing track/trigger with component creation
  • Assuming track triggers updates directly
2. Which of the following is the correct syntax to import track and trigger from Vue's reactivity package?
easy
A. import { track, trigger } from 'vue';
B. import track, trigger from 'vue';
C. import { track, trigger } from '@vue/reactivity';
D. import * as reactivity from 'vue';

Solution

  1. Step 1: Identify correct import source

    The manual reactivity APIs track and trigger are exported from '@vue/reactivity', not directly from 'vue'.
  2. Step 2: Correct import syntax

    Using named imports with curly braces is required: import { track, trigger } from '@vue/reactivity';
  3. Final Answer:

    import { track, trigger } from '@vue/reactivity'; -> Option C
  4. Quick Check:

    Correct import = import { track, trigger } from '@vue/reactivity'; [OK]
Hint: Use curly braces and '@vue/reactivity' for manual reactivity imports [OK]
Common Mistakes:
  • Importing from 'vue' instead of '@vue/reactivity'
  • Missing curly braces for named imports
  • Using default import syntax incorrectly
3. Given the following code snippet, what will be logged to the console?
import { reactive, effect, track, trigger } from '@vue/reactivity';

const state = { count: 0 };

effect(() => {
  track(state, 'count');
  console.log('Count is:', state.count);
});

state.count = 1;
trigger(state, 'count');
medium
A. Count is: 0 Count is: 1
B. Count is: 0
C. Count is: 1
D. No output

Solution

  1. Step 1: Initial effect run and tracking

    The effect runs once initially, calling track(state, 'count') and logging 'Count is: 0'.
  2. Step 2: State change and manual trigger

    When state.count is set to 1, Vue does not update automatically because manual tracking is used. Calling trigger(state, 'count') manually runs the effect again, logging 'Count is: 1'.
  3. Final Answer:

    Count is: 0 Count is: 1 -> Option A
  4. Quick Check:

    Manual trigger causes effect rerun [OK]
Hint: Manual trigger reruns effect, so both old and new logged [OK]
Common Mistakes:
  • Assuming automatic update without trigger
  • Expecting only one log line
  • Confusing track with trigger
4. Identify the error in this Vue manual reactivity code snippet:
import { reactive, effect, track, trigger } from '@vue/reactivity';

const state = { value: 10 };

effect(() => {
  track(state.value);
  console.log('Value:', state.value);
});

state.value = 20;
trigger(state, 'value');
medium
A. track is called with a property value instead of the target object and key.
B. trigger is missing after state.value update.
C. effect should not be used with manual reactivity.
D. reactive is not imported correctly.

Solution

  1. Step 1: Check track usage

    track requires two arguments: the reactive target object and the property key as a string. Here, track(state.value) passes only the value (number 10), which is incorrect.
  2. Step 2: Correct usage of track

    The correct call is track(state, 'value') to track the 'value' property on the reactive object.
  3. Final Answer:

    track is called with a property value instead of the target object and key. -> Option A
  4. Quick Check:

    track needs (object, key) [OK]
Hint: track needs object and key, not the property value [OK]
Common Mistakes:
  • Passing property value instead of object and key to track
  • Forgetting to trigger after state change
  • Misusing effect with manual reactivity
5. You want to optimize a Vue app by manually controlling reactivity for a large object data. Which approach correctly tracks and triggers updates only for the status property when it changes?
hard
A. Use effect(() => console.log(data.status)) without manual track or trigger.
B. Call track(data, 'status') inside the effect and call trigger(data, 'status') after updating data.status.
C. Call track(data) without property and call trigger(data) after any update.
D. Update data.status and rely on Vue's automatic reactivity without track or trigger.

Solution

  1. Step 1: Manual tracking of specific property

    To optimize, track only the 'status' property by calling track(data, 'status') inside the effect watching it.
  2. Step 2: Trigger update after changing property

    After updating data.status, call trigger(data, 'status') to notify dependents and update UI.
  3. Final Answer:

    Call track(data, 'status') inside the effect and call trigger(data, 'status') after updating data.status. -> Option B
  4. Quick Check:

    Track and trigger specific property for manual reactivity [OK]
Hint: Track and trigger only changed property for best performance [OK]
Common Mistakes:
  • Tracking or triggering entire object instead of property
  • Relying on automatic reactivity when manual control is needed
  • Not triggering after property update
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard