Thuta Learning
Vue.js
BasicWeb DevelopmentBeginner

Reactivity with ref() and reactive()

Vue.jsLesson 520 min

What you'll walk away with

  • Use ref for primitive state
  • Use reactive for object state
  • Understand template auto-unwrapping

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.

Mental model
  1. reactive source
  2. dependency tracking
  3. updated value or UI

Use ref for primitive state

Core syntax or pattern

vue
<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

  1. 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.

  2. dependency trackinguser.points += 5

    Learn how to build reactive state with ref() and reactive() that drives changes in the UI.

  3. updated value or UI</template>

    Apply Reactivity with ref() and reactive() in a small example and test normal, edge, and failure cases.

Execution flow
  1. reactive source
  2. dependency tracking
  3. 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.

vue
<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>
You should see
Clicks: 1
Aye Aye — 15 points

Try 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
vue
<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

What best describes the core idea of Reactivity with ref() and reactive()?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Assigning to a ref in script without .value
  • Replacing a reactive object outright with a new object

Exercise

Store a shopping cart quantity in a ref and change it with two +/− buttons.

You'll know it worked when: Clicks: 1 Aye Aye — 15 points

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Reactivity with ref() and reactive() | Thuta Learning