Challenge - 5 Problems
Custom Ref Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Vue component using customRef?
Consider this Vue 3 component using
customRef to debounce input updates. What will be logged when the user types 'abc' quickly?Vue
import { defineComponent, customRef } from 'vue'; export default defineComponent({ setup() { const debounced = customRef((track, trigger) => { let timeout; let value = ''; return { get() { track(); return value; }, set(newValue) { clearTimeout(timeout); timeout = setTimeout(() => { value = newValue; trigger(); console.log('Value updated:', value); }, 200); } }; }); return { debounced }; } });
Attempts:
2 left
💡 Hint
Think about how debounce delays the update until typing stops.
✗ Incorrect
The customRef delays setting the value until 200ms after the last change. So only the final value 'abc' triggers the console.log once.
📝 Syntax
intermediate2:00remaining
Which option correctly creates a customRef that logs on get and set?
Select the code snippet that correctly implements a
customRef which logs 'Getting value' on get and 'Setting value' on set.Attempts:
2 left
💡 Hint
Remember that
track() must be called before returning the value in get, and trigger() after setting the value.✗ Incorrect
Option B calls
track() before returning the value and trigger() after setting the value, with logs in correct order. Others have logs after return or wrong order.🔧 Debug
advanced2:00remaining
Why does this customRef not trigger updates correctly?
This customRef is intended to debounce updates but Vue components don't react to changes. What is the problem?
Vue
const debounced = customRef((track, trigger) => { let value = ''; let timeout = null; return { get() { track(); return value; }, set(newValue) { clearTimeout(timeout); timeout = setTimeout(() => { value = newValue; // Missing trigger call here console.log('Updated:', value); }, 300); } }; });
Attempts:
2 left
💡 Hint
Check if Vue is notified when the value changes.
✗ Incorrect
Without calling trigger() after updating the value, Vue's reactivity system does not detect changes and won't update components.
❓ state_output
advanced2:00remaining
What is the value of count after these operations with a customRef?
Given this customRef that doubles the value on set, what is the final value of count after these lines run?
count.value = 2; count.value = 3;
Vue
import { customRef } from 'vue'; const count = customRef((track, trigger) => { let value = 0; return { get() { track(); return value; }, set(newVal) { value = newVal * 2; trigger(); } }; });
Attempts:
2 left
💡 Hint
Remember the set multiplies the input by 2 before storing.
✗ Incorrect
The last set assigns 3, which is doubled to 6. So count.value is 6.
🧠 Conceptual
expert2:00remaining
Which statement about Vue's customRef is true?
Choose the correct statement about
customRef in Vue 3.Attempts:
2 left
💡 Hint
Think about what customRef controls in Vue's reactivity.
✗ Incorrect
customRef lets you define custom tracking and triggering behavior for a ref's value. It does not automatically make deep reactivity, nor replace computed, nor is it limited to templates.