Reactive objects let Vue track changes in your data automatically. This helps your app update the screen when data changes without extra work.
Reactive objects with reactive in Vue
Introduction
Syntax
Vue
import { reactive } from 'vue'; const state = reactive({ key1: 'value1', key2: 'value2' });
Use
reactive to wrap an object so Vue can track its changes.The returned object behaves like the original but updates the UI when changed.
Examples
user with name and age. Changing user.name updates the UI.Vue
import { reactive } from 'vue'; const user = reactive({ name: 'Alice', age: 30 });
settings tracks theme and notifications preferences.Vue
const settings = reactive({ theme: 'dark', notifications: true });
Vue
user.age++;
// UI updates automatically to show new ageSample Program
This Vue component shows a user's name and age. Clicking the button increases the age. The UI updates automatically because user is reactive.
Vue
<template>
<div>
<p>User name: {{ user.name }}</p>
<p>User age: {{ user.age }}</p>
<button @click="incrementAge">Increase Age</button>
</div>
</template>
<script setup>
import { reactive } from 'vue';
const user = reactive({
name: 'Alice',
age: 30
});
function incrementAge() {
user.age++;
}
</script>Important Notes
Reactive objects track nested properties too, so changes deep inside update the UI.
Do not replace the whole reactive object directly; change its properties instead.
Use reactive for objects, and ref for single primitive values.
Summary
Reactive objects let Vue watch your data and update the UI automatically.
Use reactive to wrap objects you want to track.
Changing properties on reactive objects triggers UI updates without extra code.
Practice
1. What does the
reactive function in Vue do?easy
Solution
Step 1: Understand the purpose of
Thereactivereactivefunction wraps an object so Vue can track its property changes.Step 2: Compare options with the purpose
Only It makes an object reactive so Vue tracks its changes automatically. correctly describes this behavior; others describe unrelated Vue features.Final Answer:
It makes an object reactive so Vue tracks its changes automatically. -> Option BQuick Check:
reactive() tracks object changes = C [OK]
Hint: Remember: reactive() wraps objects to track changes [OK]
Common Mistakes:
- Confusing reactive() with component creation
- Thinking reactive() converts reactive objects back to plain objects
- Assuming reactive() adds event listeners
2. Which of the following is the correct way to create a reactive object in Vue 3?
easy
Solution
Step 1: Check the syntax for reactive object creation
The correct syntax is calling reactive() with an object:reactive({ count: 0 }).Step 2: Identify syntax errors in other options
const state = reactive = { count: 0 } uses invalid assignment, C uses array syntax incorrectly, D tries to assign property directly without reactive call.Final Answer:
const state = reactive({ count: 0 }) -> Option DQuick Check:
reactive() called with object literal = A [OK]
Hint: Use reactive() with an object inside parentheses [OK]
Common Mistakes:
- Using assignment instead of function call
- Passing array instead of object
- Assigning properties outside reactive()
3. Given this code, what will be logged to the console?
import { reactive } from 'vue'
const state = reactive({ count: 1 })
state.count = 5
console.log(state.count)medium
Solution
Step 1: Understand reactive object property update
Assigningstate.count = 5updates the reactive property value.Step 2: Check the logged value
Loggingstate.countafter update outputs the new value 5.Final Answer:
5 -> Option AQuick Check:
Updated reactive property logs new value = D [OK]
Hint: Reactive objects update properties directly and reflect changes [OK]
Common Mistakes:
- Thinking reactive objects are immutable
- Expecting old value after update
- Confusing reactive with ref syntax
4. What is wrong with this Vue 3 code snippet?
import { reactive } from 'vue'
const state = reactive({ count: 0 })
state = { count: 5 }medium
Solution
Step 1: Identify the error in variable reassignment
The variablestateis declared withconst, so it cannot be reassigned.Step 2: Understand reactive object usage
Reactive objects are mutable in their properties but the variable holding them cannot be reassigned if declared const.Final Answer:
You cannot reassign a reactive object variable directly. -> Option AQuick Check:
const variable reassignment causes error = A [OK]
Hint: const variables can't be reassigned, only their properties changed [OK]
Common Mistakes:
- Trying to reassign const reactive variables
- Confusing reactive() input types
- Thinking reactive objects restrict property types
5. You want to create a reactive object to track a user's profile with nested address info. Which code correctly makes the entire object reactive so changes to nested properties update the UI?
hard
Solution
Step 1: Understand nested reactivity with reactive()
Using reactive() on the whole object recursively makes nested properties reactive automatically.Step 2: Analyze each option's approach
const profile = reactive({ name: 'Alice', address: { city: 'NY', zip: '10001' } }) wraps the entire object including nested address, so all changes are reactive. const profile = reactive({ name: 'Alice' }); profile.address = { city: 'NY', zip: '10001' } assigns a plain object to nested property after reactive call, losing reactivity for nested changes. const profile = { name: 'Alice', address: reactive({ city: 'NY', zip: '10001' }) } mixes reactive inside plain object, which breaks reactivity tracking. const profile = reactive({ name: 'Alice', address: ref({ city: 'NY', zip: '10001' }) }) mixes ref inside reactive, which is invalid since reactive expects plain objects.Final Answer:
const profile = reactive({ name: 'Alice', address: { city: 'NY', zip: '10001' } }) -> Option CQuick Check:
reactive() wraps whole nested object = B [OK]
Hint: Wrap entire nested object with reactive() for full reactivity [OK]
Common Mistakes:
- Wrapping nested objects separately after reactive() call
- Mixing reactive and plain objects incorrectly
- Assigning plain objects to reactive properties
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
