Bird
Raised Fist0
Vueframework~20 mins

Emitting custom events in Vue - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the $emit method do in a Vue child component?
easy
A. It registers a new component globally.
B. It updates the child component's data directly.
C. It changes the route in a Vue Router setup.
D. It sends a custom event to the parent component.

Solution

  1. Step 1: Understand the role of $emit

    The $emit method is used inside a child component to send a message or event to its parent.
  2. Step 2: Identify the effect of $emit

    It triggers a custom event that the parent can listen to and respond accordingly.
  3. Final Answer:

    It sends a custom event to the parent component. -> Option D
  4. Quick Check:

    $emit sends events to parent [OK]
Hint: Remember: child uses $emit to talk to parent [OK]
Common Mistakes:
  • Thinking $emit changes child data directly
  • Confusing $emit with global component registration
  • Assuming $emit changes routes
2. Which of the following is the correct syntax to emit a custom event named update with a payload 42 inside a Vue component?
easy
A. this.$emit('update', 42)
B. emit.$('update', 42)
C. $emit('update', 42)
D. this.emit('update', 42)

Solution

  1. Step 1: Recall Vue event emission syntax

    In Vue, to emit an event from a component, you use this.$emit(eventName, payload).
  2. Step 2: Match the correct syntax

    this.$emit('update', 42) correctly uses this.$emit('update', 42), which is the proper method call.
  3. Final Answer:

    this.$emit('update', 42) -> Option A
  4. Quick Check:

    Use this.$emit to emit events [OK]
Hint: Always use this.$emit to send events in Vue [OK]
Common Mistakes:
  • Omitting 'this' before $emit
  • Using incorrect method names like emit or emit.$
  • Calling $emit without this context
3. Given this child component code:
export default {
  methods: {
    sendAlert() {
      this.$emit('alert', 'Hello!')
    }
  }
}

And the parent template:
<ChildComponent @alert="showMessage" />

What happens when sendAlert() is called?
medium
A. The parent method showMessage is called with 'Hello!' as argument.
B. Nothing happens because the event name is incorrect.
C. An error occurs because $emit cannot send strings.
D. The child component updates its own data with 'Hello!'.

Solution

  1. Step 1: Understand event emission and listening

    The child emits an event named 'alert' with the string 'Hello!'. The parent listens for 'alert' and calls showMessage.
  2. Step 2: Trace the event flow

    When sendAlert() runs, the parent receives the event and calls showMessage('Hello!').
  3. Final Answer:

    The parent method showMessage is called with 'Hello!' as argument. -> Option A
  4. Quick Check:

    Child emits event, parent listens and reacts [OK]
Hint: Child emits event, parent listens with @event [OK]
Common Mistakes:
  • Thinking event names must be different in child and parent
  • Believing $emit cannot send string data
  • Assuming child updates itself on emit
4. Identify the error in this Vue child component method:
methods: {
  notify() {
    $emit('notify')
  }
}
medium
A. Event name 'notify' is reserved and cannot be used.
B. Missing this before $emit causes a ReferenceError.
C. The method should return a value after emitting.
D. The methods object cannot contain functions.

Solution

  1. Step 1: Check method syntax for emitting events

    In Vue, $emit must be called with this context inside methods.
  2. Step 2: Identify the missing this

    The code calls $emit('notify') without this, causing a ReferenceError.
  3. Final Answer:

    Missing this before $emit causes a ReferenceError. -> Option B
  4. Quick Check:

    Always use this.$emit inside methods [OK]
Hint: Use this.$emit, not just $emit inside methods [OK]
Common Mistakes:
  • Omitting this before $emit
  • Thinking event names are restricted
  • Expecting methods to return values after emit
5. You want a child component to emit an event change only when a new value is different from the old one. Which code correctly implements this logic inside the child?
hard
A.
methods: {
  updateValue(newVal) {
    if (newVal == this.value) {
      this.$emit('change', newVal)
    }
  }
}
B.
methods: {
  updateValue(newVal) {
    this.$emit('change', newVal)
  }
}
C.
methods: {
  updateValue(newVal) {
    if (newVal !== this.value) {
      this.$emit('change', newVal)
    }
  }
}
D.
methods: {
  updateValue(newVal) {
    this.emit('change', newVal)
  }
}

Solution

  1. Step 1: Understand the condition for emitting

    The event should emit only if the new value differs from the current this.value.
  2. Step 2: Check each option's logic

    The code that uses if (newVal !== this.value) { this.$emit('change', newVal) } correctly emits only on change. Code that emits unconditionally sends every time. Code using if (newVal == this.value) emits when values match (wrong logic). Code with this.emit uses invalid syntax.
  3. Final Answer:

    methods: { updateValue(newVal) { if (newVal !== this.value) { this.$emit('change', newVal) } } } -> Option C
  4. Quick Check:

    Emit only if newVal differs from current [OK]
Hint: Emit event only when new value differs [OK]
Common Mistakes:
  • Emitting event unconditionally
  • Using == instead of !== for change detection
  • Calling this.emit instead of this.$emit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard