📖 Scenario: You are building a simple Vue 3 component that tracks the number of clicks on a button. This is a common task in web apps where you want to react to user actions and update the interface automatically.
🎯 Goal: Create a Vue 3 component using the Composition API that uses ref to hold a reactive counter. The counter should start at zero and increase by one each time the button is clicked. The displayed number updates automatically.
📋 What You'll Learn
Use the ref function from Vue to create a reactive variable called count.
Initialize count to 0.
Create a function called increment that adds 1 to count.
Bind the increment function to a button's click event.
Display the current value of count in the template.
💡 Why This Matters
🌍 Real World
Tracking user interactions like clicks is common in web apps for features like likes, votes, or counters.
💼 Career
Understanding reactive data with ref is essential for building interactive Vue 3 applications used in modern web development jobs.
Progress0 / 4 steps
1
Set up the reactive variable with ref
Import ref from 'vue' and create a reactive variable called count initialized to 0 inside the setup() function.
Vue
Hint
Use import { ref } from 'vue' and then const count = ref(0) inside <script setup>.
2
Create the increment function
Inside the setup() script, create a function called increment that increases count.value by 1.
Vue
Hint
Define function increment() { count.value++ } inside the script.
3
Bind the increment function to the button click
In the template, add a @click event to the <button> that calls the increment function when clicked.
Vue
Hint
Add @click="increment" inside the <button> tag.
4
Display the reactive count value
Update the <p> tag in the template to show the current value of count using Vue's template syntax {{ count }}.
Vue
Hint
Use {{ count }} inside the <p> tag to show the reactive value.
Practice
(1/5)
1. What does the ref function in Vue do?
easy
A. Defines a new component
B. Creates a reactive data reference that Vue tracks for changes
C. Imports external libraries
D. Sets up routing between pages
Solution
Step 1: Understand the purpose of ref
ref is used to create reactive data that Vue can track and update the UI when it changes.
Step 2: Compare options with ref usage
Options A, C, and D describe unrelated Vue features like components, imports, and routing, not reactive data.
Final Answer:
Creates a reactive data reference that Vue tracks for changes -> Option B
Quick Check:
ref creates reactive data = B [OK]
Hint: Remember: ref makes data reactive for UI updates [OK]
Common Mistakes:
Confusing ref with component creation
Thinking ref imports libraries
Mixing ref with routing setup
2. Which is the correct way to declare a reactive variable count initialized to 0 using ref in Vue?
easy
A. var count = ref;
B. const count = reactive(0);
C. const count = ref(0);
D. let count = ref = 0;
Solution
Step 1: Recall correct ref syntax
The correct syntax is const count = ref(0); to create a reactive reference with initial value 0.
Step 2: Identify incorrect options
const count = reactive(0); uses reactive incorrectly with a primitive. let count = ref = 0; has invalid assignment syntax. var count = ref; incorrectly assigns the ref function without calling it and uses var which is discouraged in modern Vue code.
Final Answer:
const count = ref(0); -> Option C
Quick Check:
Use const with ref() for reactive variables = D [OK]
Hint: Use const and call ref() with initial value [OK]
A. You cannot reassign a ref variable directly; must update .value
B. ref cannot hold string values
C. Missing parentheses when calling ref
D. No import statement for ref
Solution
Step 1: Identify how to update ref values
Ref variables are constants holding reactive objects. You cannot reassign the variable itself; instead, update the inner value with message.value = 'Hi';.
Step 2: Check other options
ref cannot hold string values is false because ref can hold strings. Missing parentheses when calling ref is false because ref is called correctly. No import statement for ref is false because import is present.
Final Answer:
You cannot reassign a ref variable directly; must update .value -> Option A
Quick Check:
Update ref.value, not ref variable = C [OK]
Hint: Change .value property, not the ref variable itself [OK]
Common Mistakes:
Reassigning ref variable instead of .value
Thinking ref can't hold strings
Missing import statement
5. You want to create a reactive counter that increments only if the current count is less than 10. Which Vue code correctly implements this using ref?
hard
A. let count = ref(0);
if (count.value <= 10) {
count.value += 1;
}
B. const count = ref(0);
if (count < 10) {
count++;
}
C. const count = reactive(0);
if (count.value < 10) {
count.value++;
}
D. const count = ref(0);
if (count.value < 10) {
count.value++;
}
Solution
Step 1: Use ref and access .value correctly
const count = ref(0);
if (count.value < 10) {
count.value++;
} correctly declares count with ref(0) and checks count.value < 10 before incrementing count.value++.
Step 2: Identify errors in other options
const count = ref(0);
if (count < 10) {
count++;
} incorrectly compares and increments count without .value. const count = reactive(0);
if (count.value < 10) {
count.value++;
} uses reactive with a primitive, which is incorrect. let count = ref(0);
if (count.value <= 10) {
count.value += 1;
} uses let (allowed but less preferred) and checks <= 10, which allows increment at 10, violating the condition.
Final Answer:
const count = ref(0);
if (count.value < 10) {
count.value++;
} -> Option D
Quick Check:
Use ref with .value and correct condition = A [OK]
Hint: Always use .value to read and write ref data [OK]
Common Mistakes:
Forgetting .value when reading or writing
Using reactive() for primitives
Incorrect comparison operators
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.