0
0
Vueframework~20 mins

Props drilling problem in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Props Drilling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be displayed by the deepest child component?

Consider a Vue 3 app where a prop is passed through multiple nested components. What text will the deepest child component render?

Vue
/* Parent.vue */
<template>
  <ChildA :message="parentMessage" />
</template>
<script setup>
const parentMessage = 'Hello from Parent'
</script>

/* ChildA.vue */
<template>
  <ChildB :message="message" />
</template>
<script setup>
const props = defineProps({ message: String })
const message = props.message
</script>

/* ChildB.vue */
<template>
  <ChildC :message="message" />
</template>
<script setup>
const props = defineProps({ message: String })
const message = props.message
</script>

/* ChildC.vue */
<template>
  <p>{{ message }}</p>
</template>
<script setup>
const props = defineProps({ message: String })
const message = props.message
</script>
AHello from Parent
BError: message is not defined
Cundefined
DEmpty paragraph with no text
Attempts:
2 left
💡 Hint

Trace how the message prop is passed down through each component.

state_output
intermediate
2:00remaining
What is the value of the prop in the grandchild after update?

In this Vue 3 example, the parent updates the prop value after 1 second. What will the grandchild component display after the update?

Vue
<template>
  <ChildA :text="parentText" />
</template>
<script setup>
import { ref, onMounted } from 'vue'
const parentText = ref('Initial')
onMounted(() => {
  setTimeout(() => {
    parentText.value = 'Updated'
  }, 1000)
})
</script>

<!-- ChildA.vue -->
<template>
  <ChildB :text="text" />
</template>
<script setup>
const props = defineProps({ text: String })
const text = props.text
</script>

<!-- ChildB.vue -->
<template>
  <p>{{ text }}</p>
</template>
<script setup>
const props = defineProps({ text: String })
const text = props.text
</script>
AInitial, then changes to Updated after 1 second
BAlways shows Initial, no change
CAlways shows Updated immediately
DThrows an error after 1 second
Attempts:
2 left
💡 Hint

Consider how Vue's reactivity works with props and refs.

🔧 Debug
advanced
2:00remaining
Why does the child component not receive the prop value?

Given this Vue 3 code, the child component shows an empty paragraph instead of the expected prop value. What is the cause?

Vue
<template>
  <Child :msg="message" />
</template>
<script setup>
const message = 'Hello'
</script>

<!-- Child.vue -->
<template>
  <p>{{ msg }}</p>
</template>
<script setup>
const props = defineProps({ msg: String })
const msg = props.msg
</script>
AMissing import of Child component in parent
BTemplate syntax error in child component
CProp name mismatch: parent passes 'msg' but child expects 'message'
DUsing <script setup> disables props
Attempts:
2 left
💡 Hint

Check the prop names used in parent and child components.

🧠 Conceptual
advanced
2:00remaining
What is a main drawback of props drilling in Vue apps?

Props drilling means passing props through many nested components that do not use them directly. What is a key problem caused by this pattern?

AIt causes Vue to throw runtime errors
BIt makes components tightly coupled and harder to maintain
CIt disables reactivity in child components
DIt prevents components from rendering
Attempts:
2 left
💡 Hint

Think about how passing props through many layers affects code clarity and maintenance.

📝 Syntax
expert
3:00remaining
Which option correctly avoids props drilling using Vue 3 features?

You want to share data deeply without passing props through many components. Which code snippet correctly uses Vue 3's provide/inject to achieve this?

Vue
/* Parent.vue */
<template>
  <ChildA />
</template>
<script setup>
import { provide, ref } from 'vue'
const sharedData = ref('Shared info')
provide('sharedKey', sharedData)
</script>

/* ChildC.vue */
<template>
  <p>{{ sharedData.value }}</p>
</template>
<script setup>
import { inject } from 'vue'
const sharedData = inject('sharedKey')
</script>
AParent uses provide inside setup but does not import provide
BParent uses provide with a ref; Child uses inject but accesses sharedData without .value
CParent uses provide with a plain string; Child uses inject expecting a ref
DParent uses provide with a ref; Child uses inject and accesses the ref value directly
Attempts:
2 left
💡 Hint

Remember that ref values need to be accessed with .value in Vue 3.