Vue Reactivity System
Estimated reading: 4 minutes 297 views

Vue Computed Properties – Efficient Reactive Data in Vue.js (2025 Guide)


Introduction – What Are Computed Properties in Vue?

In Vue.js, computed properties are reactive getters that automatically recalculate when their dependencies change. Unlike regular methods, computed properties are cached until their dependencies update, making them ideal for performance and clean code.

In this guide, you’ll learn:

  • What computed properties are and how they work
  • Differences between methods, computed, and watch
  • Syntax and real-world examples
  • Best practices and pitfalls to avoid

What Are Computed Properties?

Computed properties are defined in the computed object of a Vue component. They return a value based on existing data or props and automatically update when that data changes.

Basic Syntax:

computed: {
  fullName() {
    return this.firstName + ' ' + this.lastName;
  }
}

Explanation:

  • fullName recalculates only when firstName or lastName changes.
  • It behaves like a data property in the template.

Example – Full Name Concatenation

<template>
  <div>
    <input v-model="firstName" placeholder="First Name" />
    <input v-model="lastName" placeholder="Last Name" />
    <p>Your full name is: {{ fullName }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      firstName: 'John',
      lastName: 'Doe'
    };
  },
  computed: {
    fullName() {
      return `${this.firstName} ${this.lastName}`;
    }
  }
}
</script>

Result: fullName updates reactively as the user types, but only recalculates when dependencies change.


Computed vs Methods – Key Difference

Featurecomputedmethods
Caching Yes (memoized until data changes) No (runs every time)
Use CaseDerived valuesActions or function calls
Template Usage{{ computedName }}{{ methodName() }}

Use computed when you want cached, reactive values, and methods when performing logic each time.


When to Use Computed Properties

Derived values from data or props
Toggling UI states
Filtering or formatting lists
Managing class or style bindings
Complex template expressions


Computed Property with Conditional Logic

computed: {
  userStatus() {
    return this.isOnline ? 'Online' : 'Offline';
  }
}

Reacts automatically when isOnline changes.


Computed Properties in Class Binding

<div :class="statusClass">Status</div>
computed: {
  statusClass() {
    return this.isOnline ? 'online' : 'offline';
  }
}

Perfect for conditionally styling elements.


Computed Property with Getter and Setter

Vue allows computed properties to have both a getter and a setter:

computed: {
  fullName: {
    get() {
      return `${this.firstName} ${this.lastName}`;
    },
    set(newValue) {
      const names = newValue.split(' ');
      this.firstName = names[0];
      this.lastName = names[1];
    }
  }
}

Two-way binding enabled with v-model on computed property:

<input v-model="fullName" />

Pitfalls to Avoid

Using functions inside templates: Avoid:

<p>{{ getFullName() }}</p>

Use computed:

<p>{{ fullName }}</p>

Mutating dependencies in the computed getter
Keep computed functions pure (no side effects)


Real-World Example – Filtered List

<input v-model="search" placeholder="Search..." />

<ul>
  <li v-for="user in filteredUsers" :key="user.id">{{ user.name }}</li>
</ul>
data() {
  return {
    search: '',
    users: [
      { id: 1, name: 'Alice' },
      { id: 2, name: 'Bob' }
    ]
  };
},
computed: {
  filteredUsers() {
    return this.users.filter(user =>
      user.name.toLowerCase().includes(this.search.toLowerCase())
    );
  }
}

Efficient filtering based on search input, without unnecessary recalculations.


Summary – Recap & Next Steps

Vue computed properties are a key part of reactive programming in Vue. They provide a clean, efficient way to handle derived data without cluttering your logic or templates.

Key Takeaways:

  • Computed properties are cached and reactive
  • Ideal for derived data, conditionals, class/style bindings
  • Avoid mutations inside computed getters
  • Use setter for two-way binding if needed

Real-World Relevance:
Used in search filters, total price calculators, dynamic class toggling, and form validation logic across real Vue apps.


FAQ Section

What is a computed property in Vue?

A computed property is a reactive function that returns a value based on data or props, and recalculates only when dependencies change.


How is a computed property different from a method?

Computed properties are cached; methods execute every time they are called.


Can a computed property have a setter?

Yes. You can define get and set for two-way binding:

computed: {
  fullName: {
    get() { ... },
    set(val) { ... }
  }
}

When should I use computed vs watch?

Use computed for derived values.
Use watch to react to data changes and trigger side effects.


Can I use computed properties in v-bind?

Absolutely. Example:

<div :class="statusClass"></div>

Share Now :
Share

Vue Computed Properties

Or Copy Link

CONTENTS
Scroll to Top