Challenge - 5 Problems
Reactive Map and Set Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when updating a reactive Map in Vue?
Consider the following Vue 3 component using the Composition API and a reactive Map. What will be displayed after clicking the button once?
Vue
import { reactive } from 'vue'; export default { setup() { const map = reactive(new Map([['a', 1]])); function update() { map.set('a', map.get('a') + 1); } return { map, update }; }, template: `<div><button @click="update">Update</button><p>{{ map.get('a') }}</p></div>` }
Attempts:
2 left
💡 Hint
Remember that reactive Map tracks changes to its entries and updates the template accordingly.
✗ Incorrect
The reactive Map initially has 'a' mapped to 1. Clicking the button calls update(), which increments the value to 2. Vue tracks this change and updates the displayed value.
❓ state_output
intermediate2:00remaining
How many items are in the reactive Set after adding duplicates?
Given this Vue 3 setup with a reactive Set, what is the size of the Set after running addItems()?
Vue
import { reactive } from 'vue'; export default { setup() { const set = reactive(new Set()); function addItems() { set.add('apple'); set.add('banana'); set.add('apple'); } addItems(); return { set }; }, template: `<p>Set size: {{ set.size }}</p>` }
Attempts:
2 left
💡 Hint
Sets do not allow duplicate entries.
✗ Incorrect
The Set adds 'apple' and 'banana'. Adding 'apple' again does not increase size. So size is 2.
📝 Syntax
advanced2:00remaining
Which option correctly creates a reactive Map and updates it in Vue 3?
Select the code snippet that correctly creates a reactive Map and updates a key's value so Vue tracks the change.
Attempts:
2 left
💡 Hint
Remember that Map methods like set() must be used to update entries.
✗ Incorrect
Option B uses reactive with a Map and updates entries with set(), which Vue tracks. Options B and D try to use object syntax on Map, which doesn't work. Option B uses reactive on an object, not a Map, so set() is undefined.
🔧 Debug
advanced2:00remaining
Why does this Set not trigger updates in Vue?
This Vue 3 component uses a Set but the template does not update when items are added. What is the cause?
Vue
import { reactive } from 'vue'; export default { setup() { const set = new Set(); function addItem(item) { set.add(item); } return { set, addItem }; }, template: `<div><button @click="addItem('x')">Add</button><p>Count: {{ set.size }}</p></div>` }
Attempts:
2 left
💡 Hint
Check if Vue tracks changes made by Set methods and if the Set is reactive.
✗ Incorrect
Vue 3 tracks mutations on reactive Set instances via methods like add(), but plain Set objects (without reactive()) are not tracked by Vue's reactivity system.
🧠 Conceptual
expert3:00remaining
What is the best way to iterate a reactive Map in Vue 3 templates?
Vue 3 tracks changes to reactive Maps via set(), but to iterate over entries in a template (e.g., with v-for), which approach ensures Vue tracks changes and updates the template correctly?
Attempts:
2 left
💡 Hint
Think about how Vue handles iteration over Map/Set in templates.
✗ Incorrect
Direct v-for on Map/Set doesn't support reactive iteration. Using a computed property to convert to Array.from(map.entries()) allows Vue to track changes and re-render properly. Other options add unnecessary overhead.