You build a +1 button for an iced tea order. Every click, the console prints a number going up: 2, 3, 4. The screen? Still 1. No error, no warning.
The reason: that number lives in a plain variable, and Vue only watches reactive data. As soon as reactive data changes, the parts of the view that read it are updated. Picture the kitchen at Mrs. Sri's stall. The cook always looks at the order board on the wall. If the waiter writes "Iced Tea × 2" on that board, the cook knows right away. If the waiter only notes it in his own pocket notebook, the cook never learns that anything changed. In Vue, the cook is Vue, the board is reactive data, and the pocket notebook is a plain variable.
Why a plain variable isn't enough
Here is a +1 button that keeps the order count in a plain variable, count:
let count = 1
function add() {
count++
console.log("count:", count)
}
---template---
<p>Iced Tea × {{ count }}</p>
<button @click="add">+1</button>
This table shows the console and the screen each time the button is clicked:
| What happens | Console | Screen |
|---|---|---|
| The page shows | Iced Tea × 1 | |
| Click +1 | count: 2 | Iced Tea × 1 |
| Click +1 | count: 3 | Iced Tea × 1 |
| Click +1 | count: 4 | Iced Tea × 1 |
The screen stays at 1. The variable really goes up, and the console proves it. But nothing tells Vue that count changed. So Vue doesn't update the view, and the <p> keeps showing the value from when the page first appeared.
ref(): a box that Vue watches
Here is the same button, but count is made with ref:
const count = ref(1)
function add() {
count.value++
console.log("count:", count.value)
}
---template---
<p>Iced Tea × {{ count }}</p>
<button @click="add">+1</button>
Now the screen goes up too:
| What happens | count.value |
Screen |
|---|---|---|
| The page shows | 1 | Iced Tea × 1 |
| Click +1 | 2 | Iced Tea × 2 |
| Click +1 | 3 | Iced Tea × 3 |
ref(1)wraps the number 1 in a box, which is an object with one property namedvalue. Vue watches that box.- In the script, you read and change what's inside through
.value:count.value++. Another example: withconst name = ref("Vue"),console.log(name.value)printsVue. Butconsole.log(name)prints the box, not the text. - In the template, Vue opens the box itself, so
{{ count }}is enough. That's also why@click="count++"written right in the template works. constis fine: only the box's content changes, never the box.- A
refcan hold anything: a number, text, a boolean, an array or an object. - In a real project,
refis imported first:import { ref } from "vue". In the exercise editorrefis already available, so the line is optional there.
reactive(): an object watched directly
For objects, Vue has a second way. Here is a cart that holds a menu name and its count:
const cart = reactive({ menu: "Iced Tea", count: 1 })
function add() {
cart.count++
}
---template---
<p>{{ cart.menu }} × {{ cart.count }}</p>
<button @click="add">+1</button>
reactive()takes an object or an array and returns a version Vue watches. Its properties are used directly, without.value.reactive()is for objects only.reactive(0)makes nothing reactive. In development mode, the console writes[Vue warn] value cannot be made reactive: 0. For numbers and text, useref.- Don't take it apart. Taking it apart means copying the object's properties into separate variables, for example
let { count } = cart. This line copies the number 1 into a plain variable.count++only changes the copy:cart.countstays 1, and the screen doesn't change. If you really need to take it apart, usetoRefs.const { count } = toRefs(cart)gives arefthat stays connected tocart, socount.value++changescart.counttoo. - Don't replace it whole. If
cartis made withlet,cart = reactive({ ... })makes a new object, while the view is still connected to the old one. Withconst, as in this example, the line stops right away with aTypeError. Change its properties one by one.
ref or reactive?
ref() |
reactive() |
|
|---|---|---|
| Holds | anything | an object or an array |
| In the script | count.value |
cart.count |
| In the template | {{ count }} |
{{ cart.count }} |
| Replaced whole | safe: count.value = 5 |
cut off from the view |
| Taken apart | not needed | cut off, unless through toRefs |
The Vue docs suggest ref() as your main choice, because it has one rule that works for every kind of data. reactive() suits an object you change one property at a time.
⚠️ A misplaced .value, and data cut off from the screen
- Forgetting
.valuein the script. Withconst count = ref(1), writingcount++tries to replace the box. JavaScript stops withTypeError: Assignment to constant variable.In the script, writecount.value++. - Writing
.valuein the template. In the template,countis already a number. A number has no.value, so{{ count.value }}shows nothing. Just{{ count }}. - A plain variable for data that changes.
let count = 1shows once, then never changes on the screen. In the exercises, the checker makes sure your code callsref(...)orreactive(...). - Taking
reactiveapart. The variables you get are only copies, and Vue doesn't watch copies. Usecart.countdirectly, ortoRefs.
TL;DR: Data that changes the view has to be reactive (watched by Vue). A plain variable can change without telling Vue, so the screen stays the same. const count = ref(1) makes a box (an object with one property, value) that Vue watches. In the script, you read and change it through count.value. In the template, just {{ count }}. For a ref named name, console.log(name.value) prints what's inside. reactive({ ... }) is for objects only, and its properties are used without .value. Don't take it apart into separate variables with let { count } = ..., and don't replace it whole, because its connection to the view breaks. When in doubt, use ref().
⚡ A plain variable isn't watched
Write:
let count = 1
function add() {
count++
console.log("count:", count)
}
---template---
<p>Iced Tea × {{ count }}</p>
<button @click="add">+1</button>
Guess first: +1 is clicked three times, and the console prints 2, 3, 4. What does the screen show?
Still Iced Tea × 1, with no error. The variable really goes up, but nothing tells Vue, so Vue doesn't update the view.
Meaning: Vue only watches reactive data, such as data made with ref(). Here count is a plain variable: its value changes, but the screen stays the same.
⚡ ref(): a box that Vue watches
Write:
const count = ref(1)
function add() {
count.value++
}
---template---
<p>Iced Tea × {{ count }}</p>
<button @click="add">+1</button>
Meaning: ref(1) wraps the number 1 in a box that Vue watches. In the script, you read and change what's inside through .value. In the template, Vue opens the box itself, so {{ count }} is enough. Now every click on +1 also raises the number on the screen.
⚡ reactive() for an object
Write:
const cart = reactive({ menu: "Iced Tea", count: 1 })
Guess first: the screen shows cart.count. If you write let { count } = cart, then count++, does the screen change?
No. count is only a copy of the number 1 in a plain variable, so cart.count stays 1. If you really need a property in a separate variable, use toRefs.
Meaning: reactive() makes an object that Vue watches. Unlike ref(), reactive() is only for objects or arrays, and its properties are used directly, without .value: cart.count++. When in doubt, use ref().