📖 Scenario: You are building a simple Vue app to track a collection of favorite fruits and a set of selected fruits. You want to use Vue's reactive Map and Set to keep the UI updated automatically when the data changes.
🎯 Goal: Create a Vue component that uses reactive Map and Set to store fruit names and their colors, and track which fruits are selected. Display the fruits and highlight the selected ones.
📋 What You'll Learn
Create a reactive Map called fruits with exact entries: 'apple' => 'red', 'banana' => 'yellow', 'grape' => 'purple'
Create a reactive Set called selectedFruits initially empty
Add a method toggleSelection that adds a fruit to selectedFruits if not present, or removes it if already selected
Render a list of fruit names with their colors, and highlight the selected fruits with a CSS class
💡 Why This Matters
🌍 Real World
Reactive Map and Set are useful when you want to track collections of data with unique keys or unique items and have the UI update automatically when these collections change.
💼 Career
Understanding reactive collections in Vue helps you build dynamic interfaces that respond instantly to user actions, a common requirement in modern web development jobs.
Progress0 / 4 steps
1
Create reactive Map with fruits and colors
In the <script setup> section, import reactive from 'vue' and create a reactive Map called fruits with these exact entries: 'apple' => 'red', 'banana' => 'yellow', 'grape' => 'purple'.
Vue
Hint
Use reactive(new Map([...])) to create a reactive Map with the given fruit-color pairs.
2
Create reactive Set for selected fruits
Below the fruits Map, create a reactive Set called selectedFruits that starts empty.
Vue
Hint
Use reactive(new Set()) to create an empty reactive Set.
3
Add toggleSelection method to update selectedFruits
Add a function called toggleSelection that takes a fruit name as argument. If selectedFruits has the fruit, remove it; otherwise, add it.
Vue
Hint
Use selectedFruits.has(fruit) to check presence, then delete or add accordingly.
4
Render fruit list with selection highlight
In the <template>, use v-for to loop over fruits.entries(). Display each fruit name and color. Add a class called selected if the fruit is in selectedFruits. Add a @click event on each fruit to call toggleSelection with the fruit name.
Vue
Hint
Use v-for with fruits.entries(), bind class with selectedFruits.has(fruit), and add @click to call toggleSelection.
Practice
(1/5)
1. Which Vue function should you use to make a Map reactive so that UI updates when it changes?
easy
A. computed()
B. ref()
C. watch()
D. reactive()
Solution
Step 1: Understand Vue reactivity basics
Vue's reactive() wraps objects like Map or Set to track changes.
Step 2: Identify correct function for Map reactivity
Only reactive() can wrap Map to make it reactive, so UI updates on mutations.
Final Answer:
reactive() -> Option D
Quick Check:
Wrap Map with reactive() = C [OK]
Hint: Use reactive() to wrap Map or Set for reactivity [OK]
Common Mistakes:
Using ref() which works for primitives, not Map/Set
Confusing computed() with reactive()
Trying watch() to make Map reactive
2. Which of the following is the correct syntax to create a reactive Set in Vue?
easy
A. const mySet = reactive(new Set())
B. const mySet = ref(new Set())
C. const mySet = reactive(Set())
D. const mySet = reactive([])
Solution
Step 1: Check how to create reactive Set
You must create a new Set instance and wrap it with reactive().
Step 2: Identify correct syntax
reactive(new Set()) is correct; Set() without new is invalid, and ref() wraps primitives.
Final Answer:
const mySet = reactive(new Set()) -> Option A
Quick Check:
Use new Set() inside reactive() = B [OK]
Hint: Always use new Set() inside reactive() for reactive Sets [OK]