JavaScript hooks for transitions let you run code at specific moments during an animation. This helps you control what happens when elements enter or leave the page.
You want to start a sound or effect exactly when an element appears.
You need to fetch data right after an animation finishes.
You want to clean up or reset something when an element disappears.
You want to coordinate multiple animations with custom timing.
You want to add or remove classes dynamically during transitions.
Syntax
Vue
<transition
@before-enter="hookBeforeEnter"
@enter="hookEnter"
@after-enter="hookAfterEnter"
@before-leave="hookBeforeLeave"
@leave="hookLeave"
@after-leave="hookAfterLeave"
>
<!-- element to animate -->
</transition>
Each hook is a JavaScript function that runs at a specific transition phase.
Use component to wrap the element you want to animate.
Examples
Runs the onEnter function when the element starts entering.
This Vue component shows a box that fades in and out when you click the button. The JavaScript hooks control the fade effect by changing opacity step by step. Console logs show when each hook runs.
Hooks receive the element as the first argument and a done callback for async animations.
Use done() to tell Vue when your animation finishes, so it can proceed.
Console logs help you see the order of hook calls during transitions.
Summary
JavaScript hooks let you run code at key moments in transitions.
Use hooks to customize animations beyond CSS.
Hooks help coordinate complex effects and side actions during enter/leave.
Practice
(1/5)
1. What is the main purpose of JavaScript hooks in Vue transitions?
easy
A. To run custom code at specific moments during enter and leave transitions
B. To replace CSS animations completely
C. To define the HTML structure of a component
D. To manage Vue component state
Solution
Step 1: Understand what JavaScript hooks do in Vue transitions
JavaScript hooks allow you to run code at key moments during the enter and leave phases of a transition.
Step 2: Compare options with this understanding
Only To run custom code at specific moments during enter and leave transitions correctly describes this purpose. Options B, C, and D describe unrelated concepts.
Final Answer:
To run custom code at specific moments during enter and leave transitions -> Option A
Quick Check:
JavaScript hooks = run code during transitions [OK]
Hint: Hooks run code during enter/leave, not replace CSS [OK]
Common Mistakes:
Thinking hooks replace CSS animations
Confusing hooks with component state management
Believing hooks define HTML structure
2. Which of the following is the correct syntax to define a JavaScript hook for the beforeEnter phase in a Vue transition component?
easy
A. <transition v-on:before-enter="myHook">
B. <transition v-on:beforeEnter="myHook">
C. <transition @before-enter="myHook">
D. <transition @beforeEnter="myHook">
Solution
Step 1: Recall Vue event naming conventions for transitions
Vue uses kebab-case event names for transition hooks, like before-enter, and the shorthand @ for v-on:.
Step 2: Match the correct syntax
<transition @before-enter="myHook"> uses @before-enter, which is correct. Options A, C, and D use camelCase or incorrect casing.
Final Answer:
<transition @before-enter="myHook"> -> Option C
Quick Check:
Vue transition hooks use kebab-case with @ [OK]
Hint: Use kebab-case and @ for Vue transition hooks [OK]
Common Mistakes:
Using camelCase event names
Mixing v-on: and @ incorrectly
Capitalizing event names
3. Given this Vue transition component snippet, what will be logged when the element starts entering?
A. Calling done() immediately before animation finishes causes transition to end too soon
B. Missing a return statement in onLeave function
C. Using setTimeout inside onLeave is not allowed
D. v-if should be v-show for transitions
Solution
Step 1: Understand the leave hook timing
The onLeave hook should call done() only after the animation finishes to keep the element during transition.
Step 2: Analyze the code's done() call
Here, done() is called immediately, but the opacity change happens after 500ms, so the transition ends too soon.
Final Answer:
Calling done() immediately before animation finishes causes transition to end too soon -> Option A
Quick Check:
done() must wait for animation end [OK]
Hint: Call done() only after animation completes [OK]
Common Mistakes:
Calling done() too early
Confusing v-if and v-show for transitions
Thinking setTimeout is disallowed in hooks
5. You want to create a fade transition that delays the leave animation until a custom API call finishes. Which approach correctly uses JavaScript hooks to achieve this?
<transition @leave="handleLeave">
<div v-if="show">Content</div>
</transition>
<script setup>
const show = ref(true)
async function apiCall() {
return new Promise(resolve => setTimeout(resolve, 1000))
}
function handleLeave(el, done) {
// What should go here?
}
</script>
hard
A. Set opacity to 0, call done(), then call apiCall()
B. Call apiCall(), then in .then() set opacity to 0 and call done()
C. Call done() immediately, then call apiCall()
D. Do nothing in handleLeave; rely on CSS only
Solution
Step 1: Understand the goal
The leave transition should wait for the API call to finish before starting the fade out and completing the transition.
Step 2: Use async logic in the leave hook
Call apiCall(), then after it resolves, set opacity to 0 and call done() to signal transition end.
Final Answer:
Call apiCall(), then in .then() set opacity to 0 and call done() -> Option B
Quick Check:
Wait async, then animate and done() [OK]
Hint: Wait async call before done() in leave hook [OK]
Common Mistakes:
Calling done() before async finishes
Ignoring async and relying on CSS only
Calling done() too early, skipping animation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.