📖 Scenario: You are building a simple to-do list app where users can add and remove tasks. To make the experience smooth and visually appealing, you want to animate the list items as they appear and disappear.
🎯 Goal: Create a Vue component that uses <TransitionGroup> to animate a list of tasks when they are added or removed.
📋 What You'll Learn
Create a reactive list of tasks in the component's data.
Add a configuration variable to hold the CSS transition name.
Use <TransitionGroup> with the configured transition name to wrap the list items.
Add buttons to add and remove tasks to see the animations in action.
💡 Why This Matters
🌍 Real World
Animating lists improves user experience in apps like to-do lists, shopping carts, and message threads by making changes smooth and clear.
💼 Career
Understanding how to use Vue's TransitionGroup is valuable for frontend developers building interactive and polished user interfaces.
Progress0 / 4 steps
1
Set up the initial list of tasks
Create a reactive variable called tasks using ref with the initial array ["Buy groceries", "Walk the dog", "Read a book"] inside the <script setup> block.
Vue
Hint
Use import { ref } from 'vue' and then const tasks = ref([...]) to create a reactive list.
2
Add a transition name configuration
Inside the <script setup> block, create a constant called transitionName and set it to the string "fade".
Vue
Hint
Just create a constant with the exact name transitionName and assign it the string "fade".
3
Use TransitionGroup to animate the list
In the <template>, replace the comment with a <TransitionGroup> component that uses :name="transitionName". Inside it, use a <li> for each task with v-for="(task, index) in tasks" and :key="task + index". Display the task text inside each <li>.
Vue
Hint
Use <TransitionGroup> with :name="transitionName" and a v-for on <li> inside it.
4
Add buttons to add and remove tasks
Below the <TransitionGroup>, add two buttons: one with @click="tasks.push('New Task')" labeled Add Task, and another with @click="tasks.pop()" labeled Remove Task.
Vue
Hint
Add two buttons with the exact click handlers to add and remove tasks from the tasks array.
Practice
(1/5)
1. What is the main purpose of using <TransitionGroup> in Vue?
easy
A. To animate list items when they are added, removed, or moved
B. To fetch data asynchronously before rendering a list
C. To validate form inputs inside a list
D. To create a static list without any animations
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 A
Quick Check:
TransitionGroup animates list changes = B [OK]
Hint: TransitionGroup is for smooth list animations only [OK]
Common Mistakes:
Thinking TransitionGroup fetches data
Confusing TransitionGroup with form validation
Assuming it creates static lists without animation
2. Which of the following is the correct way to specify the wrapper element for a <TransitionGroup> in Vue?
easy
A. <TransitionGroup element="ul">
B. <TransitionGroup wrapper="ul">
C. <TransitionGroup tag="ul">
D. <TransitionGroup container="ul">
Solution
Step 1: Recall the correct attribute for wrapper element
The tag attribute sets the HTML element used as the wrapper in <TransitionGroup>.
Step 2: Verify the options
Only <TransitionGroup tag="ul"> uses tag="ul", which is the correct syntax. Others use invalid attributes like wrapper, element, or container.
Final Answer:
<TransitionGroup tag="ul"> -> Option C
Quick Check:
Wrapper element uses tag attribute = C [OK]
Hint: Use tag="element" to set wrapper in TransitionGroup [OK]
Common Mistakes:
Using wrapper or element instead of tag
Omitting the tag attribute and expecting default
Confusing with other component props
3. Given this Vue template snippet, what will be the rendered output when the items array is ["A", "B"]?
The <TransitionGroup> uses tag="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 A
Quick Check:
Unique keys + tag="ul" = animated <ul> list [OK]
Hint: Check tag and keys to predict output and animation [OK]
Common Mistakes:
Assuming default wrapper is div instead of ul
Forgetting to add unique keys causing no animation
Thinking missing key causes syntax error
4. Identify the error in this Vue code using <TransitionGroup>:
<TransitionGroup tag="ul" name="slide">
<li v-for="item in items">{{ item }}</li>
</TransitionGroup>
medium
A. Incorrect tag attribute value; should be div
B. Missing unique :key attribute on list items
C. name attribute cannot be used with TransitionGroup
D. v-for syntax is invalid
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 :key attribute to track changes for animation.
Step 2: Validate other attributes
The tag="ul" and name="slide" are valid. The v-for syntax is correct.
Final Answer:
Missing unique :key attribute on list items -> Option B
Quick Check:
Unique keys required for TransitionGroup lists = A [OK]
Hint: Always add :key on v-for items inside TransitionGroup [OK]
Common Mistakes:
Ignoring the need for :key causing animation issues
Thinking tag must be div
Believing name attribute is invalid
5. You want to animate a list of tasks with <TransitionGroup> using a custom CSS animation named "bounce". The tasks array may have duplicate names. Which approach ensures smooth animations without glitches?
hard
A. Do not use keys and rely on default behavior with name="bounce"
B. Use :key="task" on list items and set name="bounce" on TransitionGroup
C. Use :key="index" on list items and set name="bounce" on TransitionGroup
D. Use :key="task + index" on list items and set name="bounce" on TransitionGroup
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
Setting name="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 D