Reactive Data — Vue

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 =…

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

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>

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

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().

What you will learn