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/animation-key-for-rerender.md
1---2title: Use Key Attribute to Force Re-render Animations3impact: MEDIUM4impactDescription: Without key attributes, Vue reuses DOM elements and animation libraries like AutoAnimate cannot detect changes to animate5type: gotcha6tags: [vue3, animation, key, autoanimate, rerender, dom]7---89# Use Key Attribute to Force Re-render Animations1011**Impact: MEDIUM** - Vue optimizes performance by reusing DOM elements when possible. However, this optimization can prevent animation libraries (like AutoAnimate) from detecting changes, because the element is updated in place rather than re-created. Adding a `:key` attribute forces Vue to treat changed elements as new, triggering proper animations.1213## Task Checklist1415- [ ] Add `:key` to elements that should animate when their content changes16- [ ] Use unique, changing values for keys (not indices)17- [ ] For route transitions, add `:key="$route.fullPath"` to `<router-view>`18- [ ] Apply `v-auto-animate` to the parent element of keyed children1920**Problematic Code:**21```vue22<template>23<!-- BAD: Text changes but no animation occurs -->24<div v-auto-animate>25<p>{{ message }}</p> <!-- No key - element is reused -->26</div>2728<!-- BAD: Image source changes but no animation -->29<div v-auto-animate>30<img :src="imageUrl" /> <!-- No key - element is reused -->31</div>3233<!-- BAD: Route changes don't animate -->34<router-view v-auto-animate /> <!-- No key -->35</template>3637<script setup>38import { ref } from 'vue'3940const message = ref('Hello')41const imageUrl = ref('/images/photo1.jpg')4243// Changing these won't trigger animations because44// Vue updates the existing elements rather than replacing them45</script>46```4748**Correct Code:**49```vue50<template>51<!-- GOOD: Key forces re-render, triggering animation -->52<div v-auto-animate>53<p :key="message">{{ message }}</p>54</div>5556<!-- GOOD: Image animates when source changes -->57<div v-auto-animate>58<img :key="imageUrl" :src="imageUrl" />59</div>6061<!-- GOOD: Route changes animate properly -->62<router-view :key="$route.fullPath" v-auto-animate />63</template>6465<script setup>66import { ref } from 'vue'6768const message = ref('Hello')69const imageUrl = ref('/images/photo1.jpg')7071// Now changing these will trigger animations72function updateMessage() {73message.value = 'World' // Triggers enter animation for new <p>74}75</script>76```7778## Why This Works7980When Vue sees a `:key` change:811. It considers the old element and new element as different822. The old element is removed (triggering leave animation)833. A new element is created (triggering enter animation)8485Without `:key`:861. Vue sees the same element type in the same position872. It updates the element's properties in place883. No DOM addition/removal occurs, so no animation triggers8990## Common Use Cases9192### Animating Text Content Changes9394```vue95<template>96<div v-auto-animate>97<h1 :key="title">{{ title }}</h1>98<p :key="description">{{ description }}</p>99</div>100</template>101```102103### Animating Dynamic Components104105```vue106<template>107<div v-auto-animate>108<component :is="currentComponent" :key="currentComponent" />109</div>110</template>111```112113### Animating Route Transitions114115```vue116<template>117<router-view v-slot="{ Component, route }">118<div v-auto-animate>119<component :is="Component" :key="route.fullPath" />120</div>121</router-view>122</template>123```124125## With Vue's Built-in Transition126127The same principle applies to Vue's `<Transition>` component:128129```vue130<template>131<!-- GOOD: Key triggers transition on content change -->132<Transition name="fade" mode="out-in">133<p :key="message">{{ message }}</p>134</Transition>135136<!-- GOOD: Different keys for conditional content -->137<Transition name="fade" mode="out-in">138<div v-if="isLoading" key="loading">Loading...</div>139<div v-else key="content">{{ content }}</div>140</Transition>141</template>142```143144## Caution: Performance Implications145146Using `:key` forces full component re-creation. For frequently changing data:147- The entire component tree under the keyed element is destroyed and recreated148- Any component state is lost149- Consider whether the animation is worth the performance cost150151```vue152<!-- Be cautious with complex components -->153<ComplexDashboard :key="refreshTrigger" />154<!-- This destroys and recreates the entire dashboard! -->155```156157## Reference158- [Vue.js Animation Techniques](https://vuejs.org/guide/extras/animation.html)159- [AutoAnimate with Vue](https://auto-animate.formkit.com/#usage-vue)160- [Vue.js v-for with key](https://vuejs.org/guide/essentials/list.html#maintaining-state-with-key)161