Challenge - 5 Problems
Custom Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a custom directive updates an element?
Consider a Vue 3 component using a custom directive that changes the background color of an element when the directive's value changes. What will be the background color after the value updates from 'red' to 'blue'?
Vue
const app = Vue.createApp({ data() { return { color: 'red' }; }, template: `<div v-color-bg="color">Hello</div>` }); app.directive('color-bg', { beforeMount(el, binding) { el.style.backgroundColor = binding.value; }, updated(el, binding) { el.style.backgroundColor = binding.value; } }); const vm = app.mount('#app'); // Then later in code: vm.color = 'blue';
Attempts:
2 left
💡 Hint
Think about which directive lifecycle hook runs when the bound value changes.
✗ Incorrect
The 'updated' hook in the custom directive runs whenever the bound value changes, so the background color updates from red to blue.
🧠 Conceptual
intermediate1:30remaining
Why use custom directives instead of methods for DOM manipulation?
Which reason best explains why custom directives are preferred for direct DOM manipulation in Vue over using methods inside components?
Attempts:
2 left
💡 Hint
Think about separation of concerns between DOM and data logic.
✗ Incorrect
Custom directives are designed to handle direct DOM manipulation cleanly, keeping component templates and logic focused on data and state.
📝 Syntax
advanced2:00remaining
Identify the correct syntax for a Vue 3 custom directive definition
Which option shows the correct way to define a Vue 3 custom directive that logs a message when the element is mounted?
Attempts:
2 left
💡 Hint
Check Vue 3 directive lifecycle hook names.
✗ Incorrect
In Vue 3, the correct hook for when the element is inserted into the DOM is 'mounted'. The 'bind' and 'created' hooks are from Vue 2 and invalid here.
🔧 Debug
advanced2:00remaining
Why does this custom directive fail to update on value change?
Given this directive code, why does the element's text color not update when the bound value changes?
app.directive('text-color', {
beforeMount(el, binding) {
el.style.color = binding.value;
}
});
Attempts:
2 left
💡 Hint
Consider which lifecycle hooks run when the value changes.
✗ Incorrect
'beforeMount' runs only once before the element is inserted. To react to value changes, the 'updated' hook must be implemented.
❓ state_output
expert2:30remaining
What is the final text content after directive and reactive state update?
In this Vue 3 example, what will be the final text content of the
element after the button is clicked? const app = Vue.createApp({ data() { return { count: 0 }; }, template: `
Initial
` }); app.directive('text-update', { mounted(el, binding) { el.textContent = `Count is ${binding.value}`; }, updated(el, binding) { el.textContent = `Count is ${binding.value}`; } }); app.mount('#app');Attempts:
2 left
💡 Hint
Think about how the directive updates textContent on reactive changes.
✗ Incorrect
The directive updates the textContent on mount and on updates. Clicking the button increments count, triggering the updated hook to change the text.