๐ง 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
, andwatch
- 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 whenfirstName
orlastName
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
Feature | computed | methods |
---|---|---|
Caching | โ Yes (memoized until data changes) | โ No (runs every time) |
Use Case | Derived values | Actions 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 :