📖 Scenario: You are building a simple Vue 3 app to track a user's profile information. You want to practice using ref and reactive from the Composition API to manage state.
🎯 Goal: Create a Vue 3 component using the Composition API that stores a user's name as a ref and the user's address as a reactive object with city and country. Display these values in the template.
📋 What You'll Learn
Use ref to create a reactive variable called name with the value 'Alice'.
Use reactive to create a reactive object called address with city set to 'Paris' and country set to 'France'.
Display name, address.city, and address.country in the template.
Use the <script setup> syntax for the component.
💡 Why This Matters
🌍 Real World
Managing reactive state in Vue 3 components is essential for building interactive web apps that respond to user input and data changes.
💼 Career
Understanding <code>ref</code> and <code>reactive</code> is fundamental for Vue developers to write clean, maintainable, and efficient component logic.
Progress0 / 4 steps
1
Setup the Vue component and import Composition API
Create a Vue 3 component using <script setup>. Import ref and reactive from 'vue'. Initialize a ref variable called name with the value 'Alice'.
Vue
Hint
Use import { ref, reactive } from 'vue' and then const name = ref('Alice').
2
Create a reactive object for address
Add a reactive object called address with properties city set to 'Paris' and country set to 'France'.
Vue
Hint
Use const address = reactive({ city: 'Paris', country: 'France' }).
3
Add template to display the data
Add a <template> section that displays the name, address.city, and address.country values inside paragraphs.
Vue
Hint
Use interpolation like {{ name }} inside paragraphs in the template.
4
Complete the component with proper structure
Ensure the component has a single root element in the <template>. Wrap the paragraphs inside a <section> element for semantic structure.
Vue
Hint
Wrap all paragraphs inside a single <section> element.
Practice
(1/5)
1. In Vue's Composition API, what is the main difference between ref and reactive?
easy
A. ref is used to create components, reactive is used to create directives.
B. ref is only for arrays, reactive is only for strings.
C. ref is for simple values accessed with .value, reactive is for objects and arrays accessed directly.
D. ref and reactive are exactly the same and can be used interchangeably.
Solution
Step 1: Understand ref usage
ref holds simple reactive values like numbers or strings and requires accessing the value with .value.
Step 2: Understand reactive usage
reactive wraps objects or arrays to make their properties reactive and accessed directly without .value.
Final Answer:
ref is for simple values accessed with .value, reactive is for objects and arrays accessed directly. -> Option C
Quick Check:
ref vs reactive difference = D [OK]
Hint: Remember: ref = simple value with .value, reactive = object direct access [OK]
Common Mistakes:
Thinking ref works directly on objects without .value
Confusing reactive as only for primitive values
Assuming ref and reactive are interchangeable
2. Which of the following is the correct way to declare a reactive object in Vue 3 Composition API?
easy
A. const state = ref({ count: 0 })
B. const state = reactive({ count: 0 })
C. const state = reactive(0)
D. const state = ref(0)
Solution
Step 1: Identify correct reactive syntax for objects
reactive is used to wrap objects to make their properties reactive.
Step 2: Check each option
const state = ref({ count: 0 }) uses ref with an object, which is allowed but requires .value to access. const state = reactive({ count: 0 }) correctly uses reactive with an object. const state = reactive(0) uses reactive with a number, which is invalid. const state = ref(0) uses ref with a number, which is valid but not for objects.
Final Answer:
const state = reactive({ count: 0 }) -> Option B
Quick Check:
reactive object syntax = C [OK]
Hint: Use reactive() for objects, ref() for primitives [OK]
count is a ref holding 0, incremented by count.value++ to 1. state is reactive object with clicks 0, incremented by state.clicks++ to 1.
Step 2: Check console.log output
Logging count.value and state.clicks will print 1 1.
Final Answer:
1 1 -> Option D
Quick Check:
Increment ref and reactive values = A [OK]
Hint: Increment ref with .value, reactive directly [OK]
Common Mistakes:
Forgetting to use .value with ref
Assuming reactive needs .value
Mixing up initial values
4. What is wrong with this Vue Composition API code?
import { ref } from 'vue';
const user = ref({ name: 'Alice' });
user.name = 'Bob';
medium
A. You must use user.value.name to update the name property.
B. ref cannot hold objects, use reactive instead.
C. You should use user.value() to access the object.
D. There is no error; the code works fine.
Solution
Step 1: Understand ref with objects
ref wraps the object, so to access or update properties inside, you must use .value.
Step 2: Identify the error in property update
The code tries to update user.name directly, which is incorrect. It should be user.value.name.
Final Answer:
You must use user.value.name to update the name property. -> Option A
Quick Check:
Access ref object properties with .value = A [OK]
Hint: Use .value to access or update ref object properties [OK]
Common Mistakes:
Accessing ref object properties without .value
Confusing ref and reactive usage
Thinking ref cannot hold objects
5. You want to create a reactive state for a form with fields name (string) and tags (array). You also want to track a simple counter for submissions. Which setup correctly uses ref and reactive together?
hard
A. const form = reactive({ name: '', tags: [] }); const submitCount = ref(0);
B. const form = ref({ name: '', tags: [] }); const submitCount = reactive(0);
C. const form = reactive(''); const submitCount = ref([]);
D. const form = ref(''); const submitCount = reactive({ count: 0 });
Solution
Step 1: Identify correct usage for form object
The form has multiple fields, so reactive is best to wrap the object with name and tags properties.
Step 2: Identify correct usage for simple counter
The submission count is a simple number, so ref is appropriate to track it reactively.
Final Answer:
const form = reactive({ name: '', tags: [] }); const submitCount = ref(0); -> Option A
Quick Check:
Use reactive for objects, ref for simple values = B [OK]
Hint: Use reactive for objects/arrays, ref for simple values [OK]
Common Mistakes:
Using ref for complex objects without .value
Using reactive on primitive values
Mixing up which data needs ref or reactive
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.