--- title: Use Key Attribute to Force Transition on Same Element Type impact: HIGH impactDescription: Without a key, Vue reuses the same DOM element and no transition occurs when only the content changes type: gotcha tags: [vue3, transition, key, animation, dom-reuse] --- # Use Key Attribute to Force Transition on Same Element Type **Impact: HIGH** - When transitioning between elements of the same type (e.g., two `` elements or two instances of the same component), Vue's DOM diffing algorithm will reuse the existing element and only update its content. This means no transition occurs because the element is never actually inserted or removed. You must use the `key` attribute to force Vue to treat them as distinct elements. This is one of the most common causes of "my transition isn't working" issues. ## Task Checklist - [ ] When transitioning between same element types, always add a unique `key` - [ ] For dynamic content in the same element, bind `key` to the changing value - [ ] For `v-if`/`v-else` with same element types, add distinct keys to each - [ ] Remember: without `key`, only text content updates, no animation **Problematic Code:** ```vue ``` ```vue ``` **Correct Code:** ```vue ``` ```vue ``` ```vue ``` ## Animated Counter Example ```vue ``` ## Tab Switching with Same Component ```vue ``` ## When Keys Are NOT Needed Keys are not necessary when transitioning between **different** element types: ```vue ``` ## Common Symptoms Without Key 1. No animation when content changes 2. Text updates instantly instead of fading 3. Enter/leave CSS classes never applied 4. DevTools shows element content changing but no transition state ## Reference - [Vue.js Transition - Between Elements](https://vuejs.org/guide/built-ins/transition.html#transition-between-elements)