Reactive Data
Reactivity is the most fundamental concept in Vue 3.
In this chapter you will understand what reactivity is, learn to use ref and reactive to manage data, and implement category filtering.
What is reactivity?
Reactivity means: when data changes, the page updates automatically.
The traditional JS approach is: after modifying a variable, you still have to manually update the DOM.
Vue 3's reactivity system automatically tracks data changes and notifies the view to re-render—you only need to care about the data, not the DOM.
Example
Click count: {{ count }}
+1
After clicking the button, the number shown on the page changes automatically—that is reactivity.
You don't need to write any document.querySelector or innerHTML to update the page.
ref — Handling Primitive Types
ref is the most commonly used reactivity API in Vue 3, suitable for wrapping primitive type data.
Primitive types include: string, number, boolean, null, undefined.
Syntax
Example
Name: {{ name }}
Age: {{ age }}
VIP:{{ isVip }}
The most critical point to remember:In JS code you must add .value; in templates you don't.. This is the most common mistake beginners make.
reactive — Handling Objects and Arrays
reactive is suitable for wrapping reference type data such as objects or arrays.
Unlike ref, reactive does not need .value; you can access properties directly.
Example
Name: {{ user.name }}
Age: {{ user.age }}
-
{{ hobby }}
Two Limitations of reactive
| Limitation | Description | Solution |
|---|---|---|
| Can only wrap object types | Cannot accept primitive types like string, number, etc. | Use ref for primitive types |
| Cannot be replaced as a whole | Cannot assign the entire object to a new object | Use Object.assign() or switch to ref |
Example
import
{
reactive
,
ref
}
from
'vue'
// Wrong: you cannot replace a reactive object as a whole
const
state
=
reactive
(
{
count
:
0
}
)
// state = { count: 1 } // This won't work; it loses reactivity
// Correct approach 1: modify properties instead of replacing the whole object
state.
count
=
1
// Correct approach 2: wrap the object with ref (ref allows whole replacement)
const
stateRef
=
ref
(
{
count
:
0
}
)
stateRef.
value
=
{
count
:
1
}
// This works! Because ref's .value can be replaced
When to Use ref vs reactive
| Scenario | Recommend | Example |
|---|---|---|
| A single string / number / boolean | ref | const name = ref('Xiaoming') |
| A single array | ref | const list = ref([]) |
| A single object | Either ref or reactive works | const user = ref({}) |
| Multiple unrelated states | Use a separate ref for each | One ref per line |
| A group of strongly related states | reactive | E.g.: form data, configuration item collections |
In real development, the most common practice is: always use ref, including for objects and arrays.
This keeps the writing style consistent, so you don't need to switch mental models between ref and reactive.
The rest of this tutorial uniformly uses ref to manage all state; where you need
.valueis needed, it will be specifically marked.
computed — Computed Properties
computed is "data derived from existing data."
It has two core features: caching—when dependencies don't change, it won't recompute; automatic tracking—when dependencies change, it updates automatically.
Example
Total {{ totalCount }} articles, currently showing {{ filteredCount }}
All
Vue
JavaScript
CSS
{{ article.title }}
{{ article.category }}
Whenever activeCategory changes,filteredArticles it automatically recalculates and the page refreshes automatically.
Note: computed returns a ref, so accessing it in JS also requires
.value. But in the example above we used it directly in the template, where it is automatically unwrapped.
Hands-on: Add Category Filtering to the Blog
Apply computed to the blog homepage to filter articles when a category button is clicked.
Example
Latest Articles
Total {{ filteredArticles.length }} articles
{{ article.category }}
{{ article.title }}
{{ article.summary }}
{{ article.date }}
Now clicking different category buttons filters the article list in real time. You don't need to write any DOM manipulation code—just focus on the data change logic.
Chapter Summary
In this chapter you mastered three core APIs of Vue 3's reactivity system: ref wraps primitives (use .value in JS), reactive wraps objects, and computed derives computed data.
Through computed + category filter buttons, you experienced the "data-driven view" development pattern—change the data and the page updates automatically.