--- title: useAttrs() Object Is Not Reactive impact: MEDIUM impactDescription: Watching attrs directly does not trigger - use onUpdated() or convert to props type: gotcha tags: [vue3, attrs, reactivity, composition-api] --- # useAttrs() Object Is Not Reactive **Impact: MEDIUM** - The object returned by `useAttrs()` is NOT reactive. While it always reflects the latest fallthrough attributes, you cannot use `watch()` or `watchEffect()` to observe its changes. Watchers on attrs properties will NOT trigger when attributes change. ## Task Checklist - [ ] Never use `watch()` to observe attrs changes - it won't trigger - [ ] Use `onUpdated()` lifecycle hook for side effects based on attrs - [ ] Convert frequently-accessed attrs to props if you need reactivity - [ ] Remember attrs ARE always current in templates and event handlers **Incorrect:** ```vue ``` **Correct:** ```vue ``` ```vue ``` ## Why Attrs Are Not Reactive Vue's official documentation states: > "Note that although the attrs object here always reflects the latest fallthrough attributes, it isn't reactive (for performance reasons). You cannot use watchers to observe its changes." This is a deliberate design decision for performance - making attrs reactive would add overhead to every component that uses fallthrough attributes. ## When Attrs DO Reflect Current Values Despite not being reactive, attrs always have current values in these contexts: ```vue ``` ## Computed Properties with Attrs Computed properties that reference attrs will update when the component re-renders: ```vue ``` Note: The computed updates because the component re-renders when props/attrs change, not because attrs is reactive. ## Alternative: Use getCurrentInstance() (Advanced) For advanced use cases, you can access attrs through the component instance: ```vue ``` > **Warning:** `getCurrentInstance()` is an internal API. Prefer `onUpdated()` or converting to props. ## Reference - [Fallthrough Attributes - Accessing in JavaScript](https://vuejs.org/guide/components/attrs.html#accessing-fallthrough-attributes-in-javascript) - [Vue 3 Reactivity Fundamentals](https://vuejs.org/guide/essentials/reactivity-fundamentals.html)