0
0
Vueframework~20 mins

Emitting custom events in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Custom Events Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Vue component emits a custom event?

Consider this Vue 3 component using the Composition API. What will be the output in the parent component when the button is clicked?

Vue
<script setup>
import { defineEmits } from 'vue'
const emit = defineEmits(['update'])
function handleClick() {
  emit('update', 'Hello from child')
}
</script>
<template>
  <button @click="handleClick">Click me</button>
</template>
AThe button click triggers a console log inside the child but no event is emitted.
BThe component throws an error because defineEmits is not a function.
CNothing happens because the event name 'update' is reserved and ignored.
DThe parent receives the 'update' event with payload 'Hello from child' and can react accordingly.
Attempts:
2 left
💡 Hint

Think about how Vue components communicate upward using events.

📝 Syntax
intermediate
1:30remaining
Which option correctly emits a custom event with multiple arguments in Vue 3?

Choose the correct syntax to emit a custom event named 'save' with two arguments: id and data.

Aemit('save', [id, data])
Bemit('save', { id, data })
Cemit('save', id, data)
Demit('save', id + data)
Attempts:
2 left
💡 Hint

Vue emits can send multiple arguments separately.

🔧 Debug
advanced
2:30remaining
Why does this Vue 3 component fail to emit the custom event?

Look at the code below. The parent never receives the 'submit' event. What is the cause?

Vue
<script setup>
const emit = defineEmits(['submit'])
function onSubmit() {
  emit('submit')
}
</script>
<template>
  <button @click="onSubmit">Submit</button>
</template>
AThe component is missing the import statement for <code>defineEmits</code> from 'vue'.
BThe event name 'submit' is reserved and cannot be emitted.
CThe <code>emit</code> function is called outside the <code>setup</code> function.
DThe button click handler is not correctly bound to the <code>onSubmit</code> function.
Attempts:
2 left
💡 Hint

Check if all Vue functions are properly imported.

state_output
advanced
2:00remaining
What is the parent's state after receiving this emitted event?

A child component emits a 'change' event with a payload object { count: 5 }. The parent listens and updates its total state by adding count. What is the value of total after the event?

Vue
<template>
  <Child @change="handleChange" />
  <p>Total: {{ total }}</p>
</template>
<script setup>
import { ref } from 'vue'
const total = ref(0)
function handleChange(payload) {
  total.value += payload.count
}
</script>
Aundefined
B5
CNaN
D0
Attempts:
2 left
💡 Hint

Think about how the reactive ref updates with the event payload.

🧠 Conceptual
expert
3:00remaining
Why use custom events instead of props for child-to-parent communication in Vue?

Which reason best explains why emitting custom events is preferred over directly modifying parent props in Vue components?

AProps are read-only in Vue; emitting events respects one-way data flow and keeps components decoupled.
BEmitting events automatically updates the parent props without extra code.
CProps can only be strings, so events are needed for complex data.
DCustom events allow the child to access and modify the parent's internal state directly.
Attempts:
2 left
💡 Hint

Think about Vue's design principle for data flow between components.