Explain it simply
Reactivity with ref() and reactive(), in plain language
ref() wraps a value in a reactive wrapper that you read and write via .value in JavaScript. reactive() turns an object into a proxy. In the template, you can use a ref directly — no .value needed.
Problem → Why → Solution
The problem — why this concept matters
Assigning to a ref in script without .value
A common mistake
Replacing a reactive object outright with a new object
Core concept — understand how it works
Learn how to build reactive state with ref() and reactive() that drives changes in the UI.
- reactive source
- dependency tracking
- updated value or UI
Use ref for primitive state
Core syntax or pattern
<script setup>
import { ref, reactive } from 'vue'
const count = ref(0)
const user = reactive({ name: 'Aye Aye', points: 10 })
function addPoint() {
count.value++
user.points += 5
}
</script>
<template>
<button @click="addPoint">Clicks: {{ count }}</button>
<p>{{ user.name }} — {{ user.points }} points</p>
</template>Learn how to build reactive state with ref() and reactive() that drives changes in the UI.
Read the code step by step
- reactive source
<script setup>ref() wraps a value in a reactive wrapper that you read and write via .value in JavaScript. reactive() turns an object into a proxy. In the template, you can use a ref directly — no .value needed.
- dependency tracking
user.points += 5Learn how to build reactive state with ref() and reactive() that drives changes in the UI.
- updated value or UI
</template>Apply Reactivity with ref() and reactive() in a small example and test normal, edge, and failure cases.
- reactive source
- dependency tracking
- updated value or UI
Success
Use ref for primitive state
Failure
Assigning to a ref in script without .value
Let's break it down simply
ref() wraps a value in a reactive wrapper that you read and write via .value in JavaScript. reactive() turns an object into a proxy. In the template, you can use a ref directly — no .value needed.
<script setup>
import { ref, reactive } from 'vue'
const count = ref(0)
const user = reactive({ name: 'Aye Aye', points: 10 })
function addPoint() {
count.value++
user.points += 5
}
</script>
<template>
<button @click="addPoint">Clicks: {{ count }}</button>
<p>{{ user.name }} — {{ user.points }} points</p>
</template>Clicks: 1
Aye Aye — 15 pointsTry it yourself
Store a shopping cart quantity in a ref and change it with two +/− buttons.
Reactivity Fundamentals — Vue.js
Real-world usage
How to apply it in a real project
Apply Reactivity with ref() and reactive() in a small example and test normal, edge, and failure cases.
Mini challenge
Build it yourself
Store a shopping cart quantity in a ref and change it with two +/− buttons.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
<script setup>
import { ref, reactive } from 'vue'
const count = ref(0)
const user = reactive({ name: 'Aye Aye', points: 10 })
function addPoint() {
count.value++
user.points += 5
}
</script>
<template>
<button @click="addPoint">Clicks: {{ count }}</button>
<p>{{ user.name }} — {{ user.points }} points</p>
</template>Expected: Use ref for primitive state
Show hint
Apply Reactivity with ref() and reactive() in a small example and test normal, edge, and failure cases.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- reactive source → dependency tracking → updated value or UI
- Learn how to build reactive state with ref() and reactive() that drives changes in the UI.
- Assigning to a ref in script without .value
- Apply Reactivity with ref() and reactive() in a small example and test normal, edge, and failure cases.
Reactivity Fundamentals — Vue.js