📖 Scenario: You are building a simple Vue 3 component that shows a message and logs lifecycle events to the console. This helps you understand when the component is created, mounted, updated, and unmounted.
🎯 Goal: Create a Vue 3 component using the Composition API that uses lifecycle hooks onMounted, onUpdated, and onUnmounted to log messages to the console when these events happen.
📋 What You'll Learn
Create a Vue 3 component using <script setup>
Use the Composition API lifecycle hooks onMounted, onUpdated, and onUnmounted
Log specific messages to the console inside each lifecycle hook
Display a simple message in the template
💡 Why This Matters
🌍 Real World
Understanding lifecycle hooks helps developers run code at specific times in a component's life, such as fetching data when a component appears or cleaning up resources when it disappears.
💼 Career
Many Vue.js jobs require knowledge of lifecycle hooks to manage component behavior, optimize performance, and handle side effects properly.
Progress0 / 4 steps
1
Create the basic Vue component structure
Create a Vue 3 component using <script setup> and add a <template> section that displays the text "Hello from Lifecycle Demo" inside a <div>.
Vue
Hint
Start by writing the template with a div and the exact text. Then add an empty <script setup> block.
2
Import lifecycle hooks
Inside the <script setup>, import the lifecycle hooks onMounted, onUpdated, and onUnmounted from 'vue'.
Vue
Hint
Use ES module import syntax to import the three lifecycle functions from 'vue'.
3
Add lifecycle hooks with console logs
Use the imported lifecycle hooks inside <script setup> to log these exact messages to the console: - In onMounted, log "Component mounted" - In onUpdated, log "Component updated" - In onUnmounted, log "Component unmounted"
Vue
Hint
Call each lifecycle hook function with an arrow function that calls console.log with the exact message.
4
Add a reactive counter and update log
Inside <script setup>, create a reactive variable called count initialized to 0 using ref from 'vue'. Add a button in the template that increments count when clicked. The onUpdated hook should log the updated count value with the message "Component updated: count is X" where X is the current count.
Vue
Hint
Import ref from 'vue'. Use const count = ref(0). Add a button with @click="count++". Update the onUpdated hook to log the count value using a template string.
Practice
(1/5)
1. Which lifecycle hook in Vue's Composition API runs code right after the component is added to the page?
easy
A. onUpdated
B. onCreated
C. onDestroyed
D. onMounted
Solution
Step 1: Understand the lifecycle phases
The onMounted hook runs after the component is inserted into the DOM.
Step 2: Match the hook to the action
Since the question asks for the hook that runs right after adding to the page, onMounted is the correct choice.
Final Answer:
onMounted -> Option D
Quick Check:
Mounted = after insert [OK]
Hint: Remember: mounted means component is on the page [OK]
Common Mistakes:
Confusing onMounted with onCreated
Thinking onUpdated runs after mounting
Using onDestroyed for setup tasks
2. Which of these is the correct way to import and use the onUnmounted hook inside the setup() function?
easy
A. import onUnmounted from 'vue'; setup() { onUnmounted(() => { /* cleanup */ }); }
B. import { onUnmounted } from 'vue'; setup() { onUnmounted(() => { /* cleanup */ }); }
C. import { onUnmounted } from 'vue'; setup() { onUnmounted = () => { /* cleanup */ }; }
D. import { onUnmounted } from 'vue'; setup() { onUnmounted; }
Solution
Step 1: Check import syntax
The correct import uses curly braces: import { onUnmounted } from 'vue';
Step 2: Check usage inside setup
The hook is called as a function with a callback: onUnmounted(() => { /* cleanup */ });
Final Answer:
Correct import and call syntax -> Option B
Quick Check:
Import with braces + call hook [OK]
Hint: Import hooks with braces and call them as functions [OK]
Common Mistakes:
Importing without braces
Assigning hook to a variable instead of calling
Calling hook without a callback function
3. What will be logged to the console when this Vue component runs?
A. onMounted is incorrectly assigned instead of called
B. Missing import of onUnmounted
C. setup() should return a value
D. Console.log syntax is wrong
Solution
Step 1: Check how onMounted is used
The code assigns a function to onMounted instead of calling it with a callback.
Step 2: Understand correct usage
The correct usage is onMounted(() => { ... }), not assignment.
Final Answer:
onMounted is incorrectly assigned instead of called -> Option A
Quick Check:
Hooks are called, not reassigned [OK]
Hint: Use onMounted(callback), don't assign it like a variable [OK]
Common Mistakes:
Assigning hook instead of calling it
Forgetting to pass a function to onMounted
Thinking setup must always return something
5. You want to fetch data when a component mounts and cancel a timer when it unmounts using Composition API. Which code snippet correctly uses lifecycle hooks to do this?
hard
A.
import { ref, onUnmounted } from 'vue';
export default {
setup() {
const data = ref(null);
let timer = setInterval(() => console.log('tick'), 1000);
onUnmounted(() => {
clearInterval(timer);
});
return { data };
}
}