0
0
Vueframework~20 mins

Provide and inject for deep passing in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Provide/Inject Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will the nested component display?
Consider a Vue 3 app using provide in a parent and inject in a deeply nested child. What text will the DeepChild component render?
Vue
/* Parent.vue */
<template>
  <Child />
</template>
<script setup>
import { provide } from 'vue'
import Child from './Child.vue'
provide('message', 'Hello from Parent')
</script>

/* Child.vue */
<template>
  <DeepChild />
</template>
<script setup>
import DeepChild from './DeepChild.vue'
</script>

/* DeepChild.vue */
<template>
  <p>{{ injectedMessage }}</p>
</template>
<script setup>
import { inject } from 'vue'
const injectedMessage = inject('message', 'Default message')
</script>
AHello from Parent
BDefault message
Cundefined
DError: injection not found
Attempts:
2 left
💡 Hint
Remember that provide/inject works across multiple nested components if the key matches.
state_output
intermediate
2:00remaining
What is the output after updating the provided value?
Given a parent component that provides a reactive object, and a nested child that injects and displays a property, what will the child show after the parent updates the property?
Vue
/* Parent.vue */
<template>
  <Child />
  <button @click="updateMessage">Change Message</button>
</template>
<script setup>
import { reactive, provide } from 'vue'
import Child from './Child.vue'
const state = reactive({ message: 'Initial' })
provide('state', state)
function updateMessage() {
  state.message = 'Updated'
}
</script>

/* Child.vue */
<template>
  <p>{{ state.message }}</p>
</template>
<script setup>
import { inject } from 'vue'
const state = inject('state')
</script>
AShows Updated immediately on load
BInitial, then changes to Updated after button click
CAlways shows Initial, does not update
DError: state is undefined
Attempts:
2 left
💡 Hint
Reactive objects provided are shared and updates reflect in injected components.
📝 Syntax
advanced
2:00remaining
Which option correctly provides and injects a value for deep passing?
Identify the correct syntax to provide a value in a parent and inject it in a nested child component in Vue 3 Composition API.
AParent: provide('key', value); Child: const val = inject('key')
BParent: provide = ('key', value); Child: const val = inject('key')
CParent: provide('key', value); Child: const val = inject('key', defaultValue)
DParent: provide('key', value); Child: const val = inject()
Attempts:
2 left
💡 Hint
Inject can accept a default value to avoid undefined if the key is missing.
🔧 Debug
advanced
2:00remaining
Why does the injected value show undefined in the nested child?
A developer uses provide in a parent and inject in a nested child, but the injected value is undefined. What is the most likely cause?
Vue
/* Parent.vue */
<script setup>
import { provide } from 'vue'
provide('data', 'Hello')
</script>

/* DeepChild.vue */
<script setup>
import { inject } from 'vue'
const data = inject('data')
console.log(data) // undefined
</script>
AAll of the above
BThe inject key does not match the provide key
CThe child component is not a descendant of the provider
DThe provide call is outside the setup function or lifecycle, so it runs too early
Attempts:
2 left
💡 Hint
Check provide location, key names, and component hierarchy.
🧠 Conceptual
expert
3:00remaining
How does Vue's provide/inject mechanism support deep passing without prop drilling?
Explain why provide/inject is useful for passing data deeply in a component tree compared to props, and what limitations it has.
AProvide/inject is a global state management system that replaces Vuex.
BProvide/inject automatically makes all data reactive and replaces the need for props entirely.
CProvide/inject only works for immediate children and cannot pass data deeply.
DProvide/inject allows passing data directly to any descendant without intermediate props, but it is not reactive by default and should be used sparingly.
Attempts:
2 left
💡 Hint
Think about how provide/inject avoids passing props through many layers and its reactivity behavior.