📖 Scenario: You are building a simple Vue 3 component that shows a message when it is about to appear on the page and another message when it is about to be removed. This helps you understand how Vue manages component lifecycle events.
🎯 Goal: Create a Vue 3 component using the Composition API that logs messages using onBeforeMount and onBeforeUnmount lifecycle hooks.
📋 What You'll Learn
Use the onBeforeMount lifecycle hook to log 'Component is about to mount' before the component appears.
Use the onBeforeUnmount lifecycle hook to log 'Component is about to unmount' before the component disappears.
Use the <script setup> syntax with Composition API.
Render a simple message inside a <div> element.
💡 Why This Matters
🌍 Real World
Lifecycle hooks like onBeforeMount and onBeforeUnmount help manage setup and cleanup tasks in Vue components, such as starting or stopping timers, fetching data, or cleaning event listeners.
💼 Career
Understanding Vue lifecycle hooks is essential for frontend developers working with Vue.js to build efficient, maintainable, and bug-free user interfaces.
Progress0 / 4 steps
1
Create the Vue component skeleton
Write a Vue 3 Single File Component using <script setup>. Import ref from 'vue' to create a reactive message string set to 'Hello Vue!'. Render the message inside a <template> with a <div>.
Vue
Hint
Use ref to create a reactive message variable inside <script setup>.
2
Add onBeforeMount lifecycle hook
Import onBeforeMount from 'vue' and use it to log 'Component is about to mount' before the component appears.
Vue
Hint
Use onBeforeMount(() => { ... }) to run code before the component mounts.
3
Add onBeforeUnmount lifecycle hook
Import onBeforeUnmount from 'vue' and use it to log 'Component is about to unmount' before the component is removed.
Vue
Hint
Use onBeforeUnmount(() => { ... }) to run code before the component unmounts.
4
Complete the component with proper imports and template
Ensure the component uses <template> with a <div> showing {{ message }}, and the <script setup> imports ref, onBeforeMount, and onBeforeUnmount from 'vue'. The lifecycle hooks should log the correct messages.
Vue
Hint
Make sure all imports and lifecycle hooks are present and the template shows the message.
Practice
(1/5)
1. What is the main purpose of the onBeforeMount lifecycle hook in Vue?
easy
A. It runs when the component is updated.
B. It runs after the component is removed from the screen.
C. It runs only once when the app starts.
D. It runs right before the component appears on the screen.
Solution
Step 1: Understand the timing of onBeforeMount
The onBeforeMount hook runs just before the component is rendered and shown on the screen.
Step 2: Compare with other lifecycle hooks
Unlike hooks that run after removal or updates, onBeforeMount is specifically before the first render.
Final Answer:
It runs right before the component appears on the screen. -> Option D
Quick Check:
onBeforeMount = before component appears [OK]
Hint: onBeforeMount = just before showing component [OK]
Common Mistakes:
Confusing onBeforeMount with onBeforeUnmount
Thinking it runs after component removal
Mixing it with update hooks
2. Which of the following is the correct way to use onBeforeUnmount inside the setup() function in Vue 3?
easy
A. onBeforeUnmount(function) { console.log('Unmount') }
B. onBeforeUnmount = () => { console.log('Component will unmount') }
C. onBeforeUnmount(() => { console.log('Component will unmount') })
D. setup(() => { onBeforeUnmount(console.log('Unmount')) })
Solution
Step 1: Recall correct syntax for lifecycle hooks in setup()
Lifecycle hooks like onBeforeUnmount are called as functions with a callback inside setup().
Step 2: Identify correct arrow function usage
onBeforeUnmount(() => { console.log('Component will unmount') }) correctly passes an arrow function to onBeforeUnmount to run code before unmount.
Final Answer:
onBeforeUnmount(() => { console.log('Component will unmount') }) -> Option C
Quick Check:
Use onBeforeUnmount(callback) inside setup() [OK]
Hint: Use onBeforeUnmount(() => {...}) inside setup() [OK]
Common Mistakes:
Assigning onBeforeUnmount instead of calling it
Calling onBeforeUnmount with wrong syntax
Trying to use it outside setup()
3. Consider this Vue 3 component code snippet inside setup():
B. onBeforeUnmount is assigned instead of called with a callback.
C. Arrow function syntax is incorrect.
D. setup() must return a value.
Solution
Step 1: Check how onBeforeUnmount is used
The code assigns a new function to onBeforeUnmount instead of calling it with a callback.
Step 2: Correct usage of onBeforeUnmount
It should be called like onBeforeUnmount(() => { ... }) to register the cleanup function.
Final Answer:
onBeforeUnmount is assigned instead of called with a callback. -> Option B
Quick Check:
Use onBeforeUnmount(callback), not assignment [OK]
Hint: Call onBeforeUnmount(callback), don't assign it [OK]
Common Mistakes:
Assigning lifecycle hook instead of calling it
Forgetting parentheses for function call
Confusing import errors with usage errors
5. You want to track how many times a component is about to mount and unmount using onBeforeMount and onBeforeUnmount. Which code correctly updates two counters mountCount and unmountCount inside setup()?