KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🖖

Vue

Topic Hub & Articles

Vue Intro

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Get Started

10 min

Vue Directives

10 min

Vue Events

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Forms

10 min

Vue Components

10 min

Vue Props

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Methods

10 min

Vue Computed Properties

10 min

Vue Watchers

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Class and Style Bindings

10 min

Vue Conditional Rendering

10 min

Vue List Rendering

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Template Syntax

10 min

Vue Teleport

10 min

Vue Suspense

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Transitions

10 min

Vue Mixins

10 min

Vue Custom Directives

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Plugins

10 min

Vue Slots

10 min

Vue Provide / Inject

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Dynamic Components

10 min

Vue Async Components

10 min

Composition API Intro

10 min

Recap Quiz

Gated

Milestone Assessment

ref vs reactive

10 min

Lifecycle Hooks

10 min

Dependency Injection

10 min

Recap Quiz

Gated

Milestone Assessment

Computed and Watch

10 min

Template Refs

10 min

Composable Functions

10 min

Script Setup

10 min

Progress
0%

0 / 32 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
VueVue Composition API
Lesson

Lifecycle Hooks

10 min reading
Free Course

Lifecycle Hooks: Running Code Across Component Phases

Vue lifecycle hooks let you run code at specific moments in a component's life, such as after it mounts to the DOM, after state triggers a DOM update, or right before it unmounts.

In <script setup>, you import lifecycle functions like onMounted and onUnmounted from 'vue'. Notice there are no onCreated or onBeforeCreate hooks because top-level code inside <script setup> already runs during component creation.

flowchart LR
    Setup["script setup runs"] --> Mount["onMounted (DOM Ready)"]
    Mount --> Update["onUpdated (After DOM Patch)"]
    Mount --> Unmount["onUnmounted (Cleanup)"]

Options API vs Composition API Lifecycle Mapping

Options API Hook Composition API Hook When It Runs
beforeCreate / created Top-level <script setup> During component setup
beforeMount onBeforeMount() Right before first DOM render
mounted onMounted() After DOM nodes are mounted
beforeUpdate onBeforeUpdate() Before DOM patches on state change
updated onUpdated() After reactive DOM patch finishes
unmounted onUnmounted() After component leaves the DOM

Practical SFC Lifecycle Hook Example

Pairing setup logic with cleanup inside onUnmounted keeps long-lived timers and event listeners under control. This timer component starts an interval inside onMounted and clears that interval inside onUnmounted so background timers never leak memory.

<script setup>
import { ref, onMounted, onUpdated, onUnmounted } from 'vue';

const timerSeconds = ref(0);
let intervalId = null;

onMounted(() => {
  intervalId = setInterval(() => {
    timerSeconds.value++;
  }, 1000);
});

onUpdated(() => {
  console.log('DOM updated at:', timerSeconds.value);
});

onUnmounted(() => {
  if (intervalId) {
    clearInterval(intervalId);
  }
});
</script>

<template>
  <div class="timer-box">
    <h3>Active Session Timer</h3>
    <p>Elapsed Time: <strong>{{ timerSeconds }}s</strong></p>
  </div>
</template>

Common Mistakes

  • Forgetting Cleanup in onUnmounted: Leaving setInterval, window.addEventListener, or WebSocket connections open after a component unmounts causes memory leaks and duplicate callbacks.
  • Mutating State Inside onUpdated: Changing reactive state inside onUpdated() triggers another render cycle, which can trap your component in an infinite update loop. Use watch() instead.
  • Registering Hooks Asynchronously: Calling onMounted() after an await inside <script setup> fails because Vue loses track of the current component instance. Register all hooks synchronously at the top level.

Frequently Asked Questions

Where should I fetch initial API data in <script setup>?

Call your fetch function inside onMounted() for client-side data loading, or invoke it at the top level if using <Suspense> with async setup.

Can I call onMounted() multiple times in one component?

Yes. Composables and <script setup> can each register their own onMounted() and onUnmounted() callbacks, and Vue runs every registered hook in order.

Do lifecycle hooks run during Server-Side Rendering (SSR)?

No. onMounted, onUpdated, and onUnmounted only run in the browser because there is no browser DOM on the server.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Related Developer Tools & Topic Hub

Vue Topic Hub
JSON Formatter
Validate and format JSON payloads
HTML Formatter
Format HTML templates
CSS Formatter
Clean and format stylesheets
All Vue Articles
Deep-dive guides, tips & discussions
Previousref vs reactive
Continue JourneyNext: Dependency Injection

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum