Vue Provide / Inject
Vue Provide / Inject: Avoiding Prop Drilling Across Components
Vue provide and inject let an ancestor component share reactive state directly with deeply nested child components. Instead of forwarding props manually through five intermediate components—a problem called prop drilling—you provide data once at the top and inject it wherever needed.
This pattern works well for UI themes, user locale settings, form group state, or modal controllers tied to a component subtree.
flowchart TD
Root["ThemeParent.vue (provide)"]
Middle["MiddleLayout.vue (no props needed)"]
Child["DeepChild.vue (inject)"]
Root --> Middle --> Child
Root -. Direct Injection .-> Child
How provide and inject Work Together
provide(key, value): Called inside<script setup>of an ancestor component to expose a ref, reactive object, or function to its descendants.inject(key, defaultValue): Called inside<script setup>of any descendant component to read the provided value, with an optional fallback default.
Practical SFC Provide / Inject Example
In ThemeParent.vue, provide both the reactive theme ref and the toggleTheme function so child components stay reactive when the theme changes.
<script setup>
import { ref, provide } from 'vue';
import DeepChild from './DeepChild.vue';
const theme = ref('dark');
function toggleTheme() {
theme.value = theme.value === 'dark' ? 'light' : 'dark';
}
provide('themeContext', {
theme,
toggleTheme
});
</script>
<template>
<div :class="`theme-${theme}`" class="parent-box">
<h2>Ancestor Theme Provider</h2>
<button @click="toggleTheme">Toggle from Parent</button>
<DeepChild />
</div>
</template>
In DeepChild.vue, inject 'themeContext' and pass a fallback object in case the component renders outside a provider.
<script setup>
import { inject } from 'vue';
const { theme, toggleTheme } = inject('themeContext', {
theme: 'light',
toggleTheme: () => {}
});
</script>
<template>
<div class="nested-child">
<h3>Deeply Nested Child</h3>
<p>Active Theme: <strong>{{ theme }}</strong></p>
<button @click="toggleTheme">Toggle from Child</button>
</div>
</template>
Common Mistakes
- Providing a Unwrapped Primitive Value: Passing
provide('theme', theme.value)passes a plain string snapshot, breaking reactivity. Always pass therefitself (provide('theme', theme)). - Calling
provide()orinject()Inside Async Callbacks: Both functions must run synchronously during<script setup>initialization. Callinginject()insidesetTimeoutor afterawaitfails to find the active component instance. - Letting Descendants Mutate Shared State Directly: Keep state updates inside functions owned by the provider component so you can trace where changes happen.
Frequently Asked Questions
Can I provide a value globally to the whole application?
Yes. Call app.provide('appConfig', config) in src/main.js before app.mount('#app') so every component in the app can inject it.
What happens if inject() finds no matching provide()?
Vue logs a warning and returns undefined, unless you pass a second default argument like inject('theme', 'light').
Should I use provide/inject or Pinia for state management?
Use provide/inject for component-subtree state like tabs, theme wrappers, or form contexts. Use Pinia for global application state shared across unrelated routes.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Test Your Knowledge
You've completed this section! Take a quick 5-question quiz to check your understanding.