Vue.js course · Module 3: Reactivity
reactive() - Reactive Systems
In this lesson4
When working with complex system objects, reactive() is often more convenient than ref() - it's like a complete reactor control panel instead of individual gauges.
The reactor has a name, a status, a temperature and two readings: power and radiation. Five separate refs mean five declarations and .value on every line of the script, even though all this data describes a single device. reactive() lets you enclose it in one object that is reactive as a whole.
Basics of reactive()
reactive() takes an object and returns its Proxy - a mechanism built into JavaScript that intercepts every read and write of a property. Thanks to that you do not need .value, and nested objects become reactive too:
import { reactive } from 'vue'
const reactor = reactive({
name: 'Plasma Core Alpha',
status: 'ACTIVE',
temperature: 2500,
readings: {
power: 850,
radiation: 12
}
})
// Access without .value!
console.log(reactor.name) // 'Plasma Core Alpha'
reactor.temperature = 2600
// Nested objects are also reactive
reactor.readings.power = 900 // This will also trigger a panel updateThe Proxy acts like a dispatcher between you and the data: it reports every change to the system, which refreshes the dependent views. Notice what did not change - only the object returned by reactive() is reactive, not the original you passed in:
const raw = { status: 'STANDBY' }
const panel = reactive(raw)
console.log(panel === raw) // false
console.log(reactive(raw) === panel) // trueThe Proxy is a different object from the original, and changes written directly to raw will not refresh the view. Calling reactive() again on the same object returns the same Proxy, so always work with the reactive version.
reactive() vs ref()
Both tools give you reactivity, but they differ in everyday use:
| Feature | ref() | reactive() |
|---|---|---|
| For primitives | Yes | No |
| For objects | Yes (.value) | Yes (directly) |
| Destructuring | Safe (you pass the whole container) | Loses reactivity |
| Reassign | Yes (.value = newObj) | No (only properties) |
You can pass a ref to a function or take it out of an object and it stays reactive, because the whole container travels with it. From reactive(), on the other hand, you pull out bare values. The Vue docs recommend ref() as the primary API precisely because of these limitations.
Practical Example
Here is a reactor panel built on a single object. The template also reads the nested readings:
<template>
<div class="reactor-panel" style="background: #0a0e27; color: #00ff88; border: 1px solid #00b4d8;">
<h2>{{ reactor.name }}</h2>
<p>Status: {{ reactor.status }}</p>
<p>Temperature: {{ reactor.temperature }} K</p>
<p>Readings: {{ reactor.readings.power }} MW | {{ reactor.readings.radiation }} Sv/h</p>
<button @click="updateTemperature">Increase temperature</button>
</div>
</template>There is not a single .value in the template, because reactor is a reactive object, not a ref. The script creates this object and a function that changes one property:
<script setup>
import { reactive } from 'vue'
const reactor = reactive({
name: 'Plasma Core Alpha',
status: 'ACTIVE',
temperature: 2500,
readings: {
power: 850,
radiation: 12
}
})
function updateTemperature() {
reactor.temperature += 100 // Direct modification
}
</script>Each click raises the temperature by 100 K, and the panel shows the new value. The reactor variable points to the same Proxy the whole time - only its properties change.
Limitations of reactive()
The first limitation: reactive() works only with objects, arrays and the Map and Set collections. For a number, Vue prints a warning and returns it unchanged. The second one concerns replacing the whole object:
import { reactive } from 'vue'
const reactor = reactive({ name: 'Plasma Core Alpha' })
// BAD - replacing the entire object
reactor = { name: 'Plasma Core Beta' } // This won't work!
// GOOD - modifying properties
reactor.name = 'Plasma Core Beta'With const, JavaScript itself throws a TypeError (assignment to a constant). With let, the variable would point to a new, plain object while the template stayed with the old Proxy, so the connection would break. That is why you change properties, not the whole object. When new data arrives in one piece, for example from telemetry, copy it over with Object.assign(reactor, newData) - that is still a change to the properties of the same Proxy. The third limitation is destructuring:
// BAD - destructuring loses reactivity
const { name } = reactor // name is not reactive!
// GOOD - use toRefs
import { toRefs } from 'vue'
const { name } = toRefs(reactor) // name.value is reactiveDestructuring copies the current value, while toRefs creates refs linked to the properties - we will cover it in detail in the lesson about toRef and toRefs. My practice at NOVA LAB: ref() by default, and reactive() for a coherent group of fields, such as a configuration form. In the next lesson you will check how deep this reactivity goes and when it is worth making it shallow.
Remember: reactive() is a control panel behind Proxy glass - you turn the knobs, but you never swap out the whole panel.
Code for this lesson: App.vue
<script setup>
import { ref, reactive } from 'vue'
// Comparison ref() vs reactive()
// Managing oxygen tanks at NOVA LAB station
// ref() - individual values
const mainTankLevel = ref(100)
const backupTankLevel = ref(75)
const oxygenFlow = ref(2.5)
// reactive() - complex object
const oxygenSystem = reactive({
mainTank: {
level: 100,
capacity: 1000,
pressure: 150,
status: 'OPTIMAL'
},
backupTank: {
level: 75,
capacity: 500,
pressure: 120,
status: 'STANDBY'
},
flow: {
rate: 2.5,
direction: 'OUT',
consumers: 12
}
})
// Oxygen consumption simulation
let simulationInterval = null
function startSimulation() {
if (simulationInterval) return
simulationInterval = setInterval(() => {
// Using ref - we must use .value
if (mainTankLevel.value > 0) {
mainTankLevel.value -= 0.5
}
// Using reactive - direct property access
if (oxygenSystem.mainTank.level > 0) {
oxygenSystem.mainTank.level -= 0.5
if (oxygenSystem.mainTank.level < 30) {
oxygenSystem.mainTank.status = 'WARNING'
}
if (oxygenSystem.mainTank.level < 10) {
oxygenSystem.mainTank.status = 'CRITICAL'
}
}
}, 100)
}
function stopSimulation() {
if (simulationInterval) {
clearInterval(simulationInterval)
simulationInterval = null
}
}
function refill() {
// ref - we use .value
mainTankLevel.value = 100
backupTankLevel.value = 75
// reactive - direct access
oxygenSystem.mainTank.level = 100
oxygenSystem.mainTank.status = 'OPTIMAL'
oxygenSystem.backupTank.level = 75
}
// Example: we cannot replace the entire reactive object
function wrongWayToUpdate() {
// THIS WILL NOT WORK - will lose reactivity
// oxygenSystem = { ...newData }
// CORRECT WAY - update properties
oxygenSystem.mainTank.level = 100
oxygenSystem.mainTank.status = 'OPTIMAL'
}
</script>
<template>
<div class="oxygen-control">
<div class="header">
<h1>OXYGEN SYSTEM - NOVA LAB</h1>
<p class="subtitle">ref() vs reactive() - Comparison</p>
</div>
<div class="comparison-grid">
<!-- ref() section -->
<div class="section">
<h2 class="section-title">USING ref()</h2>
<div class="card">
<div class="tank-visual">
<div class="tank-fill" :style="{ height: mainTankLevel + '%' }">
<span class="tank-label">{{ mainTankLevel.toFixed(1) }}%</span>
</div>
</div>
<div class="tank-info">
<p><strong>Main tank:</strong> {{ mainTankLevel.toFixed(1) }}%</p>
<p><strong>Backup tank:</strong> {{ backupTankLevel.toFixed(1) }}%</p>
<p><strong>Flow:</strong> {{ oxygenFlow }} L/min</p>
</div>
<div class="code-example">
<pre>const level = ref(100)
// Access: level.value
level.value = 50</pre>
</div>
</div>
</div>
<!-- reactive() section -->
<div class="section">
<h2 class="section-title">USING reactive()</h2>
<div class="card">
<div class="tank-visual">
<div
class="tank-fill"
:style="{ height: oxygenSystem.mainTank.level + '%' }"
:class="oxygenSystem.mainTank.status.toLowerCase()"
>
<span class="tank-label">{{ oxygenSystem.mainTank.level.toFixed(1) }}%</span>
</div>
</div>
<div class="tank-info">
<p>
<strong>Status:</strong>
<span :class="'status-' + oxygenSystem.mainTank.status.toLowerCase()">
{{ oxygenSystem.mainTank.status }}
</span>
</p>
<p><strong>Capacity:</strong> {{ oxygenSystem.mainTank.capacity }} L</p>
<p><strong>Pressure:</strong> {{ oxygenSystem.mainTank.pressure }} bar</p>
<p><strong>Flow:</strong> {{ oxygenSystem.flow.rate }} L/min</p>
<p><strong>Consumers:</strong> {{ oxygenSystem.flow.consumers }}</p>
</div>
<div class="code-example">
<pre>const system = reactive({
mainTank: { level: 100 }
})
// Access: system.mainTank.level
system.mainTank.level = 50</pre>
</div>
</div>
</div>
</div>
<div class="controls">
<button @click="startSimulation" class="btn btn-start">
▶START SIMULATION
</button>
<button @click="stopSimulation" class="btn btn-stop">
⏸STOP
</button>
<button @click="refill" class="btn btn-refill">
REFILL
</button>
</div>
<div class="info-box">
<h3>When to use ref() vs reactive()?</h3>
<div class="comparison-table">
<div class="comparison-col">
<h4>ref()</h4>
<ul>
<li>Primitive values (string, number, boolean)</li>
<li>When you want to replace the entire value</li>
<li>Easier to debug</li>
<li>Requires .value in script</li>
</ul>
</div>
<div class="comparison-col">
<h4>reactive()</h4>
<ul>
<li>Objects and arrays</li>
<li>Grouping related data</li>
<li>Does not require .value</li>
<li>Cannot replace the entire object</li>
</ul>
</div>
</div>
</div>
</div>
</template>
<style scoped>
.oxygen-control {
min-height: 100vh;
background: #0a0e27;
color: #e0e0e0;
padding: 2rem;
font-family: 'Courier New', monospace;
}
.header {
text-align: center;
margin-bottom: 2rem;
border-bottom: 2px solid #00ff88;
padding-bottom: 1rem;
}
.header h1 {
color: #00ff88;
font-size: 2rem;
margin: 0;
text-shadow: 0 0 10px #00ff88;
}
.subtitle {
color: #00b4d8;
margin: 0.5rem 0 0 0;
}
.comparison-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
gap: 2rem;
margin-bottom: 2rem;
}
.section-title {
color: #00b4d8;
font-size: 1.3rem;
margin-bottom: 1rem;
text-align: center;
}
.card {
background: #1a1f3a;
padding: 1.5rem;
border-radius: 8px;
border: 1px solid #00b4d8;
}
.tank-visual {
width: 100%;
height: 200px;
background: #0a0e27;
border: 2px solid #00b4d8;
border-radius: 8px;
position: relative;
overflow: hidden;
margin-bottom: 1rem;
}
.tank-fill {
position: absolute;
bottom: 0;
width: 100%;
background: linear-gradient(180deg, #00ff88, #00b4d8);
transition: height 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
}
.tank-fill.warning {
background: linear-gradient(180deg, #ffaa00, #ff8800);
}
.tank-fill.critical {
background: linear-gradient(180deg, #ff4444, #cc0000);
}
.tank-label {
color: #0a0e27;
font-weight: bold;
font-size: 1.5rem;
text-shadow: 0 0 5px rgba(255,255,255,0.5);
}
.tank-info {
margin-bottom: 1rem;
}
.tank-info p {
margin: 0.5rem 0;
color: #ccc;
}
.status-optimal {
color: #00ff88;
}
.status-warning {
color: #ffaa00;
}
.status-critical {
color: #ff4444;
animation: blink 1s infinite;
}
@keyframes blink {
0%, 50%, 100% { opacity: 1; }
25%, 75% { opacity: 0.5; }
}
.code-example {
background: #0a0e27;
padding: 1rem;
border-radius: 4px;
border-left: 3px solid #00ff88;
}
.code-example pre {
margin: 0;
color: #00ff88;
font-size: 0.85rem;
overflow-x: auto;
}
.controls {
display: flex;
gap: 1rem;
margin-bottom: 2rem;
justify-content: center;
flex-wrap: wrap;
}
.btn {
padding: 1rem 2rem;
font-size: 1rem;
border: none;
border-radius: 8px;
cursor: pointer;
font-weight: bold;
transition: all 0.3s ease;
font-family: 'Courier New', monospace;
}
.btn-start {
background: linear-gradient(135deg, #00ff88, #00b4d8);
color: #0a0e27;
}
.btn-stop {
background: linear-gradient(135deg, #ffaa00, #ff8800);
color: #0a0e27;
}
.btn-refill {
background: linear-gradient(135deg, #00b4d8, #0088cc);
color: white;
}
.btn:hover {
transform: scale(1.05);
box-shadow: 0 0 20px currentColor;
}
.info-box {
background: #1a1f3a;
padding: 1.5rem;
border-radius: 8px;
border-left: 4px solid #00ff88;
}
.info-box h3 {
color: #00ff88;
margin-top: 0;
}
.comparison-table {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
margin-top: 1rem;
}
.comparison-col h4 {
color: #00b4d8;
margin-top: 0;
}
.comparison-col ul {
padding-left: 1.5rem;
color: #ccc;
}
.comparison-col li {
margin: 0.5rem 0;
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What does reactive() do in Vue 3?
2. What is an advantage of reactive() over ref() for objects?
These are 2 of 4 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Use reactive() to create an object with station data (name, level, active) and display it in the template.
- Horizontal ordering
Arrange the elements of reactive() declaration in the correct order.
- Code editor
Create an object with reactive() and add buttons that modify its properties (increase level, toggle status).
- Click in order
Arrange the steps for creating and mutating a reactive object in the correct order.
- Vertical ordering
Arrange the steps for working with reactive() in the correct order.
Tasks after this lesson
- QuestionThe reactive() function
- QuestionAccessing reactive without .value
- Coding taskPractice: reactive() object
- Put in orderreactive() syntax
- Questionreactive limitation - reassignment
- Coding taskPractice: Modifying reactive
- Click in orderCreating and mutating reactive
- Questionref() vs reactive() - primitive values
- Put in orderreactive() workflow