Reactivity lets Vue automatically update the screen when data changes. This makes building interactive apps easier and faster.
Why reactivity is Vue's core concept
Introduction
Syntax
Vue
import { ref, reactive } from 'vue'; const count = ref(0); const state = reactive({ name: 'Vue' });
ref creates a reactive single value.
reactive creates a reactive object with multiple properties.
Examples
Vue
import { ref } from 'vue'; const count = ref(0); count.value++;
Vue
import { reactive } from 'vue'; const state = reactive({ name: 'Vue', version: 3 }); state.version = 4;
Sample Program
This Vue component shows a number and a button. Clicking the button increases the number automatically on screen because of reactivity.
Vue
<template>
<div>
<p>Count: {{ count }}</p>
<button @click="increment">Add 1</button>
</div>
</template>
<script setup>
import { ref } from 'vue';
const count = ref(0);
function increment() {
count.value++;
}
</script>Important Notes
Reactivity helps keep your UI and data in sync without extra code.
Always use ref or reactive to make data reactive in Vue.
Access reactive values with .value when using ref.
Summary
Reactivity automatically updates the UI when data changes.
Use ref for single reactive values and reactive for objects.
This makes building interactive apps simpler and cleaner.
Practice
1. Why is reactivity considered the core concept in Vue?
easy
Solution
Step 1: Understand what reactivity means in Vue
Reactivity means Vue tracks data changes and updates the UI automatically without manual work.Step 2: Identify why this is important
This automatic update makes building interactive apps easier and cleaner by syncing data and UI.Final Answer:
Because it automatically updates the UI when data changes -> Option AQuick Check:
Reactivity = automatic UI update [OK]
Hint: Reactivity means UI changes with data changes [OK]
Common Mistakes:
- Thinking reactivity requires manual DOM updates
- Believing reactivity only works with static data
- Confusing reactivity with disabling interaction
2. Which Vue syntax correctly creates a reactive single value?
easy
Solution
Step 1: Recall Vue's reactive APIs
Usereffor single primitive values andreactivefor objects.Step 2: Match syntax to single value reactivity
ref(0)creates a reactive number, whilereactiveexpects an object.Final Answer:
const count = ref(0) -> Option AQuick Check:
Single value reactive = ref() [OK]
Hint: Use ref() for single reactive values [OK]
Common Mistakes:
- Using reactive() for primitive values
- Confusing useState with Vue APIs
- Wrapping primitives inside reactive() incorrectly
3. What will be the output of this Vue code snippet?
const state = reactive({ count: 0 });
state.count++;
console.log(state.count);medium
Solution
Step 1: Understand reactive object behavior
Thereactivefunction wraps the object so changes to properties are tracked.Step 2: Analyze the code increment and output
Starting from 0,state.count++increases count to 1, so logging prints 1.Final Answer:
1 -> Option BQuick Check:
Incremented reactive count = 1 [OK]
Hint: reactive objects update properties directly [OK]
Common Mistakes:
- Expecting 0 because reactive is misunderstood
- Thinking reactive returns a primitive
- Assuming an error due to missing Vue setup
4. Identify the error in this Vue reactive code:
const state = ref({ count: 0 });
state.count++;
console.log(state.count);medium
Solution
Step 1: Understand ref with objects
refwraps the entire object inside a.valueproperty.Step 2: Identify incorrect property access
Accessingstate.countis wrong; it should bestate.value.count.Final Answer:
You cannot increment a property on a ref object directly -> Option CQuick Check:
ref object properties accessed via .value [OK]
Hint: Use .value to access ref object properties [OK]
Common Mistakes:
- Trying to increment ref object properties directly
- Confusing ref and reactive usage
- Ignoring .value when using ref
5. You want to create a reactive object with nested properties and update a nested value so the UI updates automatically. Which approach is correct?
const data = ??? data.user.name = 'Alice';
hard
Solution
Step 1: Choose reactive for nested objects
reactivemakes the whole object and nested properties reactive automatically.Step 2: Confirm nested property update works
Withreactive, updatingdata.user.nametriggers UI updates without extra refs.Final Answer:
const data = reactive({ user: { name: 'Bob' } }) -> Option DQuick Check:
Nested reactive object = reactive() [OK]
Hint: Use reactive() for nested reactive objects [OK]
Common Mistakes:
- Using ref() for nested objects without .value
- Wrapping nested objects unnecessarily in reactive
- Mixing ref and reactive incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
