TransitionGroup helps animate items when you add, remove, or reorder a list. It makes changes smooth and easy to see.
TransitionGroup for lists in Vue
<template> <transition-group name="fade" tag="ul"> <li v-for="item in items" :key="item.id"> {{ item.text }} </li> </transition-group> </template> <script setup> import { ref } from 'vue' const items = ref([ { id: 1, text: 'First' }, { id: 2, text: 'Second' }, { id: 3, text: 'Third' } ]) </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter-from, .fade-leave-to { opacity: 0; } </style>
The name attribute sets the CSS classes prefix for animations.
The tag attribute defines which HTML tag wraps the list items.
div wrapper instead of ul for the list container.<transition-group name="slide" tag="div"> <div v-for="item in items" :key="item.id"> {{ item.text }} </div> </transition-group>
<transition-group name="fade" tag="ul"> <!-- Empty list --> </transition-group>
<transition-group name="fade" tag="ul"> <li v-for="item in items" :key="item.id"> {{ item.text }} </li> </transition-group>
This Vue component shows a list with buttons to add or remove items. The list uses TransitionGroup to animate items fading in and out. Each item is focusable for accessibility.
<template>
<div>
<button @click="addItem">Add Item</button>
<button @click="removeItem">Remove Item</button>
<transition-group name="fade" tag="ul" aria-label="Animated list">
<li v-for="item in items" :key="item.id" tabindex="0">
{{ item.text }}
</li>
</transition-group>
</div>
</template>
<script setup>
import { ref } from 'vue'
const items = ref([
{ id: 1, text: 'Learn Vue' },
{ id: 2, text: 'Build App' },
{ id: 3, text: 'Deploy' }
])
let nextId = 4
function addItem() {
items.value.push({ id: nextId++, text: `New Item ${nextId - 1}` })
}
function removeItem() {
items.value.pop()
}
</script>
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s ease;
}
.fade-enter-from, .fade-leave-to {
opacity: 0;
}
ul {
list-style: none;
padding: 0;
}
li {
background: #def;
margin: 0.5rem 0;
padding: 0.5rem;
border-radius: 0.25rem;
}
button {
margin-right: 0.5rem;
padding: 0.5rem 1rem;
font-size: 1rem;
cursor: pointer;
}
</style>TransitionGroup animations run when items enter, leave, or move in the list.
Use unique :key values for each item to help Vue track changes correctly.
Time complexity depends on list size; animations add small overhead but improve user experience.
TransitionGroup animates list changes smoothly.
Use name for CSS animation classes and tag for the wrapper element.
Always provide unique keys for list items to avoid animation glitches.
Practice
<TransitionGroup> in Vue?Solution
Step 1: Understand the role of <TransitionGroup>
<TransitionGroup> is designed to animate changes in lists, such as adding or removing items.Step 2: Compare with other options
Options A, C, and D describe unrelated tasks like data fetching, form validation, or static lists without animation.Final Answer:
To animate list items when they are added, removed, or moved -> Option AQuick Check:
TransitionGroup animates list changes = B [OK]
- Thinking TransitionGroup fetches data
- Confusing TransitionGroup with form validation
- Assuming it creates static lists without animation
<TransitionGroup> in Vue?Solution
Step 1: Recall the correct attribute for wrapper element
Thetagattribute sets the HTML element used as the wrapper in <TransitionGroup>.Step 2: Verify the options
Only<TransitionGroup tag="ul">usestag="ul", which is the correct syntax. Others use invalid attributes like wrapper, element, or container.Final Answer:
<TransitionGroup tag="ul"> -> Option CQuick Check:
Wrapper element uses tag attribute = C [OK]
- Using wrapper or element instead of tag
- Omitting the tag attribute and expecting default
- Confusing with other component props
items array is ["A", "B"]?
<TransitionGroup tag="ul" name="fade">
<li v-for="item in items" :key="item">{{ item }}</li>
</TransitionGroup>Solution
Step 1: Analyze the template structure
The <TransitionGroup> usestag="ul", so the wrapper is a <ul> element. The list items have unique keys from the item values.Step 2: Understand the output and animation
The list renders as <ul><li>A</li><li>B</li></ul> and uses the "fade" CSS classes for animation on changes.Final Answer:
<ul><li>A</li><li>B</li></ul> with fade animation on changes -> Option AQuick Check:
Unique keys + tag="ul" = animated <ul> list [OK]
- Assuming default wrapper is div instead of ul
- Forgetting to add unique keys causing no animation
- Thinking missing key causes syntax error
<TransitionGroup>:
<TransitionGroup tag="ul" name="slide">
<li v-for="item in items">{{ item }}</li>
</TransitionGroup>Solution
Step 1: Check for required attributes in v-for
Each item in a list rendered by v-for inside <TransitionGroup> must have a unique:keyattribute to track changes for animation.Step 2: Validate other attributes
Thetag="ul"andname="slide"are valid. The v-for syntax is correct.Final Answer:
Missing unique :key attribute on list items -> Option BQuick Check:
Unique keys required for TransitionGroup lists = A [OK]
- Ignoring the need for :key causing animation issues
- Thinking tag must be div
- Believing name attribute is invalid
<TransitionGroup> using a custom CSS animation named "bounce". The tasks array may have duplicate names. Which approach ensures smooth animations without glitches?Solution
Step 1: Understand the importance of unique keys
Keys must be unique to avoid animation glitches. Since tasks may have duplicate names, using just the task name as key is unsafe.Step 2: Combine task and index for uniqueness
Concatenating task and index (e.g.,task + index) creates unique keys even if names repeat, ensuring smooth animations.Step 3: Confirm usage of name attribute
Settingname="bounce"on <TransitionGroup> applies the correct CSS animation classes.Final Answer:
Use :key="task + index" on list items and set name="bounce" on TransitionGroup -> Option DQuick Check:
Unique keys + name="bounce" = smooth animations [OK]
- Using task name alone as key when duplicates exist
- Using index alone causing wrong animation order
- Skipping keys causing animation glitches
Ready to prove it?
Pass the test and this pattern moves unknown
