📖 Scenario: You are building a simple Vue 3 app that shows a counter and a message that updates automatically when the counter changes.
🎯 Goal: Create a Vue 3 component using the Composition API that demonstrates how Vue tracks dependencies automatically with ref and computed.
📋 What You'll Learn
Create a reactive counter variable using ref
Create a computed property that depends on the counter
Display the counter and computed message in the template
Add a button to increase the counter
💡 Why This Matters
🌍 Real World
Reactive state and computed properties are the core of building interactive user interfaces in Vue. This project shows how Vue tracks dependencies automatically to update the UI efficiently.
💼 Career
Understanding Vue's reactivity system is essential for frontend developers working with Vue.js to build modern web apps that respond instantly to user actions.
Progress0 / 4 steps
1
Setup reactive counter variable
Create a Vue 3 component with <script setup> and import ref from 'vue'. Then create a reactive variable called counter initialized to 0.
Vue
Hint
Use import { ref } from 'vue' and then const counter = ref(0) to create a reactive variable.
2
Add computed message depending on counter
Import computed from 'vue' and create a computed property called message that returns the string `Counter is ${counter.value}`.
Vue
Hint
Use computed(() => `Counter is ${counter.value}`) to create a reactive message that updates when counter changes.
3
Add template to display counter and message
Add a <template> section that shows the counter value and the message computed property inside <p> tags.
Vue
Hint
Use {{ counter }} and {{ message }} inside <p> tags to show their values.
4
Add button to increase counter
Add a button inside the <template> that increases counter.value by 1 when clicked using @click event.
Vue
Hint
Add a button with @click="counter.value++" to increase the counter when clicked.
Practice
(1/5)
1. How does Vue automatically know which parts of your app to update when data changes?
easy
A. Vue updates everything on the page every time any data changes.
B. You must manually list all variables Vue should watch for changes.
C. Vue uses a timer to check for changes every second.
D. Vue tracks reactive data dependencies during rendering using ref and computed.
Solution
Step 1: Understand Vue's reactive system
Vue uses special reactive data wrappers like ref and computed to track dependencies automatically.
Step 2: How Vue tracks dependencies
When rendering, Vue records which reactive data is accessed, so it knows what to update when data changes.
Final Answer:
Vue tracks reactive data dependencies during rendering using ref and computed. -> Option D
Quick Check:
Automatic dependency tracking = Vue tracks reactive data dependencies during rendering using ref and computed. [OK]
Hint: Vue tracks reactive data accessed during rendering [OK]
Common Mistakes:
Thinking you must manually specify dependencies
Believing Vue updates everything on any change
Assuming Vue uses timers to detect changes
2. Which of the following is the correct way to create a reactive reference in Vue 3?
easy
A. const count = reactive(0);
B. const count = ref(0);
C. const count = computed(0);
D. const count = watch(0);
Solution
Step 1: Identify the correct reactive API
In Vue 3, ref is used to create a reactive reference to a primitive value like a number.
Step 2: Check other options
reactive is for objects, computed creates derived values, and watch observes changes but does not create reactive data.
C. You must use count.value inside computed, not count directly.
D. You need to call double() instead of double.value.
Solution
Step 1: Check how ref is accessed inside computed
Refs store their value inside a .value property, so you must use count.value to get the number.
Step 2: Identify the error
The code uses count directly, which is a ref object, not the number, causing wrong behavior.
Final Answer:
You must use count.value inside computed, not count directly. -> Option C
Quick Check:
Access ref with .value inside computed = You must use count.value inside computed, not count directly. [OK]
Hint: Always use .value to access ref inside computed [OK]
Common Mistakes:
Using ref variable directly without .value
Thinking computed can't use ref
Calling computed like a function instead of .value
5. You want to create a Vue 3 component that shows a list of items and a count of how many items have a price over 10. Which approach correctly uses Vue's automatic dependency tracking?
hard
A. Use a computed property that filters the reactive items array and returns the count of items with price > 10.
B. Manually watch the items array and update a separate count variable whenever items change.
C. Use a method that counts items with price > 10 and call it only once on component mount.
D. Store the count as a normal variable and update it inside the template.
Solution
Step 1: Understand reactive data and computed
A reactive array wrapped with ref or reactive can be used inside a computed property to automatically track dependencies.
Step 2: Why computed is best here
The computed property recalculates the count whenever the items array changes, so the UI stays updated without manual intervention.
Final Answer:
Use a computed property that filters the reactive items array and returns the count of items with price > 10. -> Option A
Quick Check:
Computed auto-updates with reactive data = Use a computed property that filters the reactive items array and returns the count of items with price > 10. [OK]
Hint: Use computed to auto-update counts from reactive arrays [OK]
Common Mistakes:
Manually watching and updating counts instead of computed
Using methods that don't auto-update
Trying to update counts inside template variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.