Challenge - 5 Problems
Vue Provide/Inject Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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>
Attempts:
2 left
💡 Hint
Remember that provide/inject works across multiple nested components if the key matches.
✗ Incorrect
The
provide in the Parent component makes the value available to all descendants. The DeepChild component uses inject with the same key, so it receives 'Hello from Parent'.❓ state_output
intermediate2: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>
Attempts:
2 left
💡 Hint
Reactive objects provided are shared and updates reflect in injected components.
✗ Incorrect
Because the parent provides a reactive object, the child sees changes to its properties. Clicking the button updates the message, which the child displays.
📝 Syntax
advanced2: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.
Attempts:
2 left
💡 Hint
Inject can accept a default value to avoid undefined if the key is missing.
✗ Incorrect
Option C shows correct provide syntax and inject with a default value, which is valid and safe. Option C misses the default value, which is optional but recommended. Option C has invalid provide syntax. Option C calls inject without arguments, causing an error.
🔧 Debug
advanced2: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>
Attempts:
2 left
💡 Hint
Check provide location, key names, and component hierarchy.
✗ Incorrect
All these reasons can cause inject to return undefined: provide called outside setup, mismatched keys, or child not nested inside provider component.
🧠 Conceptual
expert3: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.
Attempts:
2 left
💡 Hint
Think about how provide/inject avoids passing props through many layers and its reactivity behavior.
✗ Incorrect
Provide/inject lets you pass data to any descendant without passing props through every intermediate component. However, the provided value is not reactive unless you provide a reactive object. It is not a global state system and should be used carefully.