Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/provide-inject-debugging-challenges.md
1---2title: Provide/Inject Has Limited DevTools Support - Plan for Debugging3impact: LOW4impactDescription: Unlike props and state, provided values are harder to trace in Vue DevTools, making debugging more challenging5type: gotcha6tags: [vue3, provide-inject, debugging, devtools, architecture]7---89# Provide/Inject Has Limited DevTools Support - Plan for Debugging1011**Impact: LOW** - While provide/inject is powerful for avoiding prop drilling, it creates less visible data flow than props. Provided values are not as easily inspectable in Vue DevTools, and tracing where a value comes from requires navigating the component tree manually.1213## Task Checklist1415- [ ] Document provided values at the provider component level16- [ ] Use descriptive Symbol descriptions for easier identification17- [ ] Consider adding development-only logging for provided state changes18- [ ] Keep provide/inject chains shallow when possible19- [ ] Prefer Pinia for complex state that needs DevTools integration2021## The Challenge2223Unlike props which are clearly visible in Vue DevTools for each component, provided values:24251. Don't show which ancestor provided them262. Require manual navigation to find the provider273. Don't show in the standard props/data panels284. Can be shadowed by closer ancestors using the same key2930## Strategies for Better Debugging3132### 1. Use Descriptive Symbol Names3334```js35// injection-keys.js3637// BETTER: Descriptive names appear in errors and debugging38export const UserAuthKey = Symbol('UserAuthenticationState')39export const ThemeConfigKey = Symbol('ThemeConfiguration')40export const FormContextKey = Symbol('FormValidationContext')4142// WORSE: Generic names are harder to trace43export const UserKey = Symbol()44export const ThemeKey = Symbol('theme')45```4647### 2. Document Providers Clearly4849```vue50<!-- AuthProvider.vue -->51<script setup>52/**53* Authentication Provider54*55* Provides:56* - UserAuthKey: Current user state (Ref<User | null>)57* - AuthActionsKey: { login, logout, refresh }58*59* Must wrap any component that needs authentication state.60*/61import { provide, ref, readonly } from 'vue'62import { UserAuthKey, AuthActionsKey } from '@/injection-keys'6364const user = ref(null)6566// ... implementation6768provide(UserAuthKey, readonly(user))69provide(AuthActionsKey, { login, logout, refresh })70</script>71```7273### 3. Development-Only Logging7475```js76// composables/useProvideWithLogging.js77import { provide, watch, getCurrentInstance } from 'vue'7879export function useProvideWithLogging(key, value, name) {80provide(key, value)8182if (import.meta.env.DEV) {83const instance = getCurrentInstance()84const componentName = instance?.type?.name || 'Unknown'8586console.log(`[Provide] ${name} provided by <${componentName}>`)8788// Log reactive changes89if (value && typeof value === 'object' && 'value' in value) {90watch(value, (newVal) => {91console.log(`[Provide] ${name} changed:`, newVal)92}, { deep: true })93}94}95}96```9798```vue99<script setup>100import { ref } from 'vue'101import { useProvideWithLogging } from '@/composables/useProvideWithLogging'102import { ThemeKey } from '@/injection-keys'103104const theme = ref('dark')105106// In development, logs when provided and when changed107useProvideWithLogging(ThemeKey, theme, 'Theme')108</script>109```110111### 4. Inject with Missing Provider Warnings112113```js114// composables/useSafeInject.js115import { inject, getCurrentInstance } from 'vue'116117export function useSafeInject(key, fallback, keyName) {118const value = inject(key, undefined)119120if (value === undefined) {121const instance = getCurrentInstance()122const componentName = instance?.type?.name || 'Unknown'123124if (import.meta.env.DEV) {125console.warn(126`[Inject] ${keyName || String(key)} not provided. ` +127`Component <${componentName}> is using fallback value. ` +128`Ensure a provider exists in the ancestor chain.`129)130}131132return typeof fallback === 'function' ? fallback() : fallback133}134135return value136}137```138139```vue140<script setup>141import { useSafeInject } from '@/composables/useSafeInject'142import { ThemeKey } from '@/injection-keys'143144// Warns in dev if no provider found145const theme = useSafeInject(ThemeKey, () => ({ mode: 'light' }), 'ThemeConfig')146</script>147```148149### 5. Create Provider Registry for Complex Apps150151```js152// utils/provider-registry.js153const providerRegistry = new Map()154155export function registerProvider(key, componentName, value) {156if (import.meta.env.DEV) {157providerRegistry.set(key, {158componentName,159value,160timestamp: Date.now()161})162}163}164165export function getProviderInfo(key) {166return providerRegistry.get(key)167}168169// For DevTools custom plugin or debugging170export function getAllProviders() {171return Object.fromEntries(providerRegistry)172}173174// Expose to window for console debugging175if (import.meta.env.DEV) {176window.__VUE_PROVIDERS__ = {177getAll: getAllProviders,178get: getProviderInfo179}180}181```182183## When to Use Pinia Instead184185If you find yourself needing extensive debugging for state:186187| Use Provide/Inject | Use Pinia |188|-------------------|-----------|189| Component library internals | Application-wide state |190| Theme/locale configuration | User session data |191| Form context | Shopping cart |192| Simple parent-child sharing | Complex state with actions |193| Plugin configuration | State that needs time-travel debugging |194195Pinia provides excellent DevTools integration with:196- State inspection197- Time-travel debugging198- Action logging199- Hot module replacement200201## Reference202- [Vue DevTools](https://devtools.vuejs.org/)203- [Pinia DevTools](https://pinia.vuejs.org/core-concepts/index.html#devtools)204