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 Tutorial
Lesson

Vue Watchers

10 min reading
Free Course

Vue Watchers: Running Side Effects with watch and watchEffect

Vue watchers let you run custom code when reactive state changes. While computed properties calculate new values, watchers exist for side effects such as fetching API data, updating localStorage, or syncing browser titles.

Vue 3 gives you two watcher functions inside <script setup>: watch() and watchEffect(). Choosing the right one helps you control when side effects run and avoid extra network calls.

flowchart TD
    Source["Reactive Ref or Getter"] --> Watcher["watch() or watchEffect()"]
    Watcher --> Effect["Async Fetch, Storage Sync, or DOM Effect"]

watch() vs watchEffect()

  • watch(source, callback): Watches explicit sources only. It runs lazily by default and gives you both the new value and the previous value.
  • watchEffect(callback): Runs right away during setup and tracks every reactive ref read inside its body automatically.

Practical SFC Watcher Example

Here, watch() reacts to searchTopic changes with { immediate: true } and onCleanup to cancel stale timers. Meanwhile, watchEffect() keeps document.title synced with count.

<script setup>
import { ref, watch, watchEffect } from 'vue';

const searchTopic = ref('vue');
const results = ref([]);
const isLoading = ref(false);
const count = ref(0);

watch(
  searchTopic,
  (newTopic, oldTopic, onCleanup) => {
    const query = newTopic.trim();
    if (!query) {
      results.value = [];
      return;
    }

    isLoading.value = true;
    const timer = setTimeout(() => {
      results.value = [`Guide: ${query}`, `Examples: ${query}`];
      isLoading.value = false;
    }, 300);

    onCleanup(() => clearTimeout(timer));
  },
  { immediate: true }
);

watchEffect(() => {
  document.title = `Clicks: ${count.value}`;
});
</script>

<template>
  <div class="search-box">
    <input v-model="searchTopic" placeholder="Search topic..." />
    <p v-if="isLoading">Loading results...</p>
    <ul v-else>
      <li v-for="item in results" :key="item">{{ item }}</li>
    </ul>

    <button @click="count++">Increment Title Count</button>
  </div>
</template>

Common Mistakes

  • Watching a Reactive Property Directly: Writing watch(state.count, cb) passes a raw number, not a reactive source. Wrap object properties in a getter function: watch(() => state.count, cb).
  • Creating Infinite Watch Loops: Never update the watched source unconditionally inside its own watch callback, or it will trigger itself endlessly.
  • Skipping Cleanup for Async Requests: Rapid typing can cause older requests to finish after newer ones. Always cancel timers or pass an AbortController signal inside onCleanup.

Frequently Asked Questions

When do I need { deep: true } in watch()?

Pass { deep: true } when watching a ref holding a nested object and you want to react to nested property mutations. Note that watching a reactive() object directly enables deep tracking automatically.

What does { immediate: true } do?

Normally watch() waits until the source changes for the first time. Adding { immediate: true } runs the callback once right away with the initial value.

When does a watcher access the updated DOM?

By default, watchers run before Vue updates the DOM. Pass { flush: 'post' } or use watchPostEffect() when your callback needs to measure updated DOM elements.

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
Lesson Recap Quiz Available

Test Your Knowledge

You've completed this section! Take a quick 5-question quiz to check your understanding.

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
PreviousVue Computed Properties
Continue JourneyTake Recap Quiz

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum