This question is about lifecycle timing and side-effect placement in Vue components. Strong answers map real tasks like fetches, DOM reads, and cleanup to the right hook and explain why hook choice affects correctness, not just style.
Use this Vue interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
What are lifecycle hooks in Vue and when are they used?Frontend interview answer
This Vue interview question tests whether you can explain Vue lifecycle hooks: DOM timing, cleanup, and hook-choice debugging, connect it to production trade-offs, and handle common follow-up questions.
- Vue lifecycle hooks: DOM timing, cleanup, and hook-choice debugging explanation without falling back to memorized docs wording
- Lifecycle and Hooks reasoning, edge cases, and production failure modes
- How you would answer the most likely Vue interview follow-up
Overview
Lifecycle hooks are Vue's timing boundaries. The practical value is not memorizing names in order; it is knowing when the DOM exists, when reactive state is ready, and when cleanup must happen so timers, listeners, or observers do not leak.
Scope guard
If the interviewer asks "Why did Vue introduce setup(), ref(), and reactive()?", switch to <a href="/vue/trivia/vue-composition-api">What are Vue Composition APIs (setup(), ref, reactive) and why were they introduced?</a>.
This page is narrower: when hooks fire and where side effects belong.
export default {
data() {
return {
message: 'Hello Vue!'
};
},
created() {
console.log('Component is created!');
},
mounted() {
console.log('Component is mounted to the DOM!');
},
updated() {
console.log('Component has re-rendered!');
},
unmounted() {
console.log('Component has been removed from the DOM!');
}
};
Each hook corresponds to a specific stage of the component lifecycle. For example, created() runs when the component is initialized, while mounted() runs when the component is added to the DOM.
Commonly Used Lifecycle Hooks
Hook | When It Runs | Typical Use Case |
|---|---|---|
beforeCreate | Before data observation and events are set up | Rarely used; low-level hook for advanced debugging |
created | After instance is created and reactive data is available | Initialize data or fetch initial data |
beforeMount | Before component is mounted to the DOM | Prepare final changes before mounting |
mounted | After the component is mounted to the DOM | Access the DOM or third-party libraries |
beforeUpdate | Before reactive data causes re-rendering | Perform checks before DOM updates |
updated | After DOM updates due to reactive data changes | React to DOM changes or perform animations |
beforeUnmount | Before the component is destroyed | Cleanup operations or cancel timers |
unmounted | After the component is destroyed and removed from DOM | Release resources, event listeners, or observers |
Lifecycle Hook Flow
1️⃣ beforeCreate → 2️⃣ created → 3️⃣ beforeMount → 4️⃣ mounted
Then for updates: 5️⃣ beforeUpdate → 6️⃣ updated<br>And finally for teardown: 7️⃣ beforeUnmount → 8️⃣ unmounted.
Task | Best hook | Why this timing matters |
|---|---|---|
Initialize data that does not need the DOM | created() / setup() | Reactive state exists, but DOM nodes do not. |
Measure an element or start a DOM-only library | mounted() / onMounted() | The DOM is guaranteed to exist only after mount. |
Read layout after a reactive update | updated() + nextTick() | beforeUpdate sees the old DOM; nextTick waits for the patched DOM. |
Stop timers, listeners, or observers | beforeUnmount() / onBeforeUnmount() | Cleanup must run before the instance disappears to avoid leaks across route changes or v-if toggles. |
Use Case Example
Fetching data when a component mounts is one of the most common lifecycle hook patterns:
export default {
data() {
return { users: [] };
},
async mounted() {
const res = await fetch('https://api.example.com/users');
this.users = await res.json();
}
};
In this example, the mounted() hook ensures the component is attached to the DOM before fetching and displaying data.
import { ref, onMounted, onUpdated, onBeforeUnmount, nextTick } from 'vue';
const panel = ref(null);
let resizeObserver;
onMounted(() => {
resizeObserver = new ResizeObserver(() => {
console.log('mounted size:', panel.value?.getBoundingClientRect().width);
});
if (panel.value) resizeObserver.observe(panel.value);
});
onUpdated(async () => {
await nextTick();
console.log('updated size:', panel.value?.getBoundingClientRect().width);
});
onBeforeUnmount(() => {
resizeObserver?.disconnect();
});
Composition API mappingmounted maps to onMounted, updated maps to onUpdated, and beforeUnmount/unmounted map to onBeforeUnmount/onUnmounted. The timing rule is the same; only the API shape changes.
Think of lifecycle hooks as checkpoints — moments in a component’s journey where you can pause, inspect, or intervene to perform logic, cleanup, or side effects.
Summary
- Lifecycle hooks are timing checkpoints across create, mount, update, and teardown.
- Use them to place side effects safely: setup/init, DOM work, and cleanup at the right stage.
- The strongest interview signal is explaining why a task belongs in one hook and not another.
Use this as one explanation rep, then continue with the Vue.js interview questions cluster or a guided prep path.