📖 Scenario: You are building a simple Vue app that shows a message and updates it automatically when the user types something new.
🎯 Goal: Create a Vue component that uses reactivity to update the displayed message as the user types in an input box.
📋 What You'll Learn
Create a reactive variable to hold the message
Bind the input box to the reactive message variable
Display the message reactively in the template
Use Vue 3 Composition API with <script setup>
💡 Why This Matters
🌍 Real World
Reactivity lets web apps update the screen instantly when users interact, like live search or chat apps.
💼 Career
Understanding Vue's reactivity is essential for building modern, responsive user interfaces in many web development jobs.
Progress0 / 4 steps
1
Set up the reactive message variable
Create a reactive variable called message using Vue's ref function and set its initial value to "Hello, Vue!".
Vue
Hint
Use ref from Vue to create a reactive variable.
2
Bind the input to the reactive message
Bind the input element's value to the reactive variable message using v-model.
Vue
Hint
Use v-model on the input to connect it to the reactive variable.
3
Display the reactive message
Display the reactive variable message inside the <p> tag using Vue's template syntax.
Vue
Hint
Use double curly braces {{ }} to show the reactive variable in the template.
4
Complete the reactive Vue component
Ensure the Vue component uses the <script setup> syntax, imports ref from 'vue', declares the reactive message variable, binds it with v-model on the input, and displays it inside the <p> tag.
Vue
Hint
Check that all parts of the component are present and correctly connected.
Practice
(1/5)
1. Why is reactivity considered the core concept in Vue?
easy
A. Because it automatically updates the UI when data changes
B. Because it requires manual DOM updates
C. Because it only works with static data
D. Because it disables user interaction
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 A
Quick 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
A. const count = ref(0)
B. const count = reactive(0)
C. const count = reactive({ value: 0 })
D. const count = useState(0)
Solution
Step 1: Recall Vue's reactive APIs
Use ref for single primitive values and reactive for objects.
Step 2: Match syntax to single value reactivity
ref(0) creates a reactive number, while reactive expects an object.
Final Answer:
const count = ref(0) -> Option A
Quick 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
A. 0
B. 1
C. undefined
D. Error
Solution
Step 1: Understand reactive object behavior
The reactive function 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.