📖 Scenario: You are building a simple Vue 3 app that shows a counter. You want to see messages in the console right before the component updates and right after it updates. This helps you understand when Vue updates the screen.
🎯 Goal: Create a Vue 3 component that uses onBeforeUpdate and onUpdated lifecycle hooks to log messages to the console when the counter changes.
📋 What You'll Learn
Create a reactive counter variable starting at 0
Add a button to increase the counter by 1 when clicked
Use onBeforeUpdate to log 'Before update: counter is X' where X is the counter value
Use onUpdated to log 'Updated: counter is X' where X is the counter value
💡 Why This Matters
🌍 Real World
Developers often need to know when a component updates to perform side effects like animations, analytics, or syncing data.
💼 Career
Understanding Vue lifecycle hooks is essential for building interactive and efficient Vue applications in professional frontend development.
Progress0 / 4 steps
1
Set up the reactive counter
Create a Vue 3 component with a reactive variable called counter initialized to 0 using ref.
Vue
Hint
Use ref(0) to create a reactive counter variable.
2
Add a button to increase the counter
Add a button inside the <template> that increases counter.value by 1 when clicked. Use @click event with an inline arrow function.
Vue
Hint
Use @click="counter.value++" on the button to increase the counter.
3
Use onBeforeUpdate to log before update
Import onBeforeUpdate from 'vue' and use it to log 'Before update: counter is ' + counter.value before the component updates.
Vue
Hint
Use onBeforeUpdate(() => { ... }) to run code before the update.
4
Use onUpdated to log after update
Import onUpdated from 'vue' and use it to log 'Updated: counter is ' + counter.value after the component updates.
Vue
Hint
Use onUpdated(() => { ... }) to run code after the update.
Practice
(1/5)
1. What is the main difference between onBeforeUpdate and onUpdated in Vue?
easy
A. onUpdated runs only once when the component mounts.
B. onBeforeUpdate runs after the screen updates; onUpdated runs before.
C. Both run at the same time during the update.
D. onBeforeUpdate runs before the screen updates; onUpdated runs after.
Solution
Step 1: Understand lifecycle timing
onBeforeUpdate triggers just before Vue updates the DOM to reflect data changes.
Step 2: Identify post-update hook
onUpdated triggers right after the DOM has been updated.
Final Answer:
onBeforeUpdate runs before the screen updates; onUpdated runs after. -> Option D
Quick Check:
Before update = onBeforeUpdate, After update = onUpdated [OK]
Hint: Remember: before update = before screen changes, after update = after screen changes [OK]
Common Mistakes:
Confusing the order of hooks
Thinking both run at the same time
Assuming onUpdated runs only once
2. Which of the following is the correct way to import and use onBeforeUpdate in a Vue 3 component?
easy
A. import { onBeforeUpdate } from 'vue'; onBeforeUpdate(() => { console.log('Before update'); });
B. import onBeforeUpdate from 'vue'; onBeforeUpdate(() => { console.log('Before update'); });
C. import { onBeforeUpdate } from 'vue'; onBeforeUpdate = () => { console.log('Before update'); };
D. import { onBeforeUpdate } from 'vue'; onBeforeUpdate.log('Before update');
Solution
Step 1: Check import syntax
Vue exports onBeforeUpdate as a named export, so use curly braces in import.
Step 2: Use hook as a function
Call onBeforeUpdate with a callback function to run code before update.
Final Answer:
import { onBeforeUpdate } from 'vue'; onBeforeUpdate(() => { console.log('Before update'); }); -> Option A
Quick Check:
Named import + callback function = import { onBeforeUpdate } from 'vue'; onBeforeUpdate(() => { console.log('Before update'); }); [OK]
Hint: Use curly braces for named imports and pass a function [OK]
Common Mistakes:
Importing without curly braces
Assigning hook to a variable instead of calling it
Trying to call a method on the hook
3. Given this Vue 3 setup code, what will be logged to the console when count changes from 0 to 1?
A. Assigning a function to onBeforeUpdate instead of calling it.
B. Missing import of onBeforeUpdate.
C. Using count.value++ instead of count++.
D. Not returning count from setup.
Solution
Step 1: Check hook usage
The code assigns a function to onBeforeUpdate instead of calling it with a callback.
Step 2: Confirm other parts
Imports are correct, count.value++ is valid, and count is returned properly.
Final Answer:
Assigning a function to onBeforeUpdate instead of calling it. -> Option A
Quick Check:
Hooks must be called, not reassigned [OK]
Hint: Call hooks with functions, don't assign them [OK]
Common Mistakes:
Assigning hooks instead of calling
Forgetting to import hooks
Misusing ref value increment
5. You want to log the previous and current value of a reactive variable score every time it updates in a Vue 3 component. Which approach correctly uses onBeforeUpdate and onUpdated to achieve this?