0
0
Vueframework~20 mins

Events for child to parent communication in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Event Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the child emits an event?
Consider a Vue 3 component where a child emits an event named update. What will the parent component do when it listens to this event?
Vue
<template>
  <ChildComponent @update="handleUpdate" />
</template>

<script setup>
function handleUpdate(payload) {
  console.log('Received:', payload)
}
</script>
AThe child component automatically updates the parent's data without any function call.
BThe parent runs the <code>handleUpdate</code> function with the event payload.
CNothing happens unless the child modifies a prop directly.
DThe parent component reloads the entire page.
Attempts:
2 left
💡 Hint
Think about how Vue handles custom events emitted by children.
📝 Syntax
intermediate
2:00remaining
Identify the correct syntax to emit an event from a child component
Which of the following is the correct way to emit an event named save with a payload { id: 1 } inside a Vue 3 child component using defineEmits?
Aconst emit = defineEmits(['save']); emit('save', { id: 1 });
Bthis.$emit('save', { id: 1 });
CemitEvent('save', { id: 1 });
DdefineEmits.emit('save', { id: 1 });
Attempts:
2 left
💡 Hint
In Vue 3 <script setup>, defineEmits returns a function to call.
🔧 Debug
advanced
2:00remaining
Why does the parent not receive the event?
A child component emits an event change, but the parent does not respond. What is the most likely cause?
Vue
<!-- Parent.vue -->
<template>
  <ChildComponent @changed="onChange" />
</template>

<script setup>
function onChange() {
  console.log('Changed event received')
}
</script>

<!-- Child.vue -->
<script setup>
const emit = defineEmits(['change'])
function trigger() {
  emit('change')
}
</script>
AThe parent method onChange is not defined properly.
BThe child forgot to call the emit function.
CThe parent listens to @changed but the child emits 'change' event; event names must match exactly.
DVue does not support custom events between components.
Attempts:
2 left
💡 Hint
Check the event names in the parent and child components.
state_output
advanced
2:00remaining
What is the parent's data after child emits an event?
Given the following parent and child components, what will be the value of message in the parent after the child emits the event?
Vue
<!-- Parent.vue -->
<template>
  <ChildComponent @send-message="updateMessage" />
  <p>{{ message }}</p>
</template>

<script setup>
import { ref } from 'vue'
const message = ref('')
function updateMessage(text) {
  message.value = text
}
</script>

<!-- Child.vue -->
<script setup>
const emit = defineEmits(['send-message'])
emit('send-message', 'Hello from child')
</script>
AAn error is thrown because emit is called outside a function
B'' (empty string)
Cundefined
D'Hello from child'
Attempts:
2 left
💡 Hint
The emit is called top-level during the child's setup phase.
🧠 Conceptual
expert
2:00remaining
Why use events for child to parent communication instead of props?
In Vue, why is emitting events from child to parent preferred over trying to modify parent props directly?
AProps are read-only in Vue; modifying them directly breaks the one-way data flow and causes errors.
BEvents are faster than props for data transfer.
CProps cannot pass any data from parent to child.
DVue automatically syncs props without events, so events are unnecessary.
Attempts:
2 left
💡 Hint
Think about Vue's data flow rules and how props are designed.