Bird
Raised Fist0
Vueframework~20 mins

JavaScript hooks for transitions in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vue Transition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when the @before-enter hook returns a Promise?
In Vue 3 transitions, if the @before-enter hook returns a Promise, how does Vue handle the transition?
Vue
<template>
  <transition
    @before-enter="beforeEnter"
    @enter="enter"
    @leave="leave"
  >
    <div v-if="show">Hello</div>
  </transition>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(true)

function beforeEnter(el) {
  return new Promise(resolve => {
    setTimeout(() => {
      el.style.opacity = '0'
      resolve()
    }, 100)
  })
}

function enter(el, done) {
  el.style.transition = 'opacity 0.5s'
  el.style.opacity = '1'
  setTimeout(done, 500)
}

function leave(el, done) {
  el.style.transition = 'opacity 0.5s'
  el.style.opacity = '0'
  setTimeout(done, 500)
}
</script>
AVue throws a runtime error because hooks cannot return Promises.
BVue ignores the Promise and starts the enter transition immediately.
CVue runs the enter transition in parallel with the Promise.
DVue waits for the Promise to resolve before starting the enter transition.
Attempts:
2 left
💡 Hint
Think about how Vue manages asynchronous hooks to control timing.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines a @enter hook with a done callback?
Select the correct syntax for an @enter hook that uses the done callback to signal transition end.
Afunction enter(done, el) { el.style.opacity = '1'; done(); }
Bfunction enter(el) { el.style.opacity = '1'; }
Cfunction enter(el, done) { el.style.opacity = '1'; done(); }
Dfunction enter(el, done) { el.style.opacity = '1'; return done; }
Attempts:
2 left
💡 Hint
The done callback is the second argument and must be called to finish the transition.
🔧 Debug
advanced
2:00remaining
Why does the leave transition never finish in this code?
Given the following leave hook, why does the transition never complete?
Vue
<template>
  <transition @leave="leave">
    <div v-if="show">Bye</div>
  </transition>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(true)

function leave(el, done) {
  el.style.transition = 'opacity 1s'
  el.style.opacity = '0'
  // done() is never called
}
</script>
AThe done callback is never called, so Vue waits indefinitely for transition end.
BThe opacity style is set incorrectly, so the transition does not start.
CThe <code>v-if</code> directive prevents the leave hook from running.
DVue requires the leave hook to return a Promise to finish.
Attempts:
2 left
💡 Hint
Check if the done callback is called to signal transition completion.
🧠 Conceptual
advanced
1:30remaining
What is the purpose of the @after-leave hook in Vue transitions?
Choose the best description of what the @after-leave hook does.
AIt runs only if the leave transition is cancelled.
BIt runs after the leave transition finishes and the element is removed from the DOM.
CIt runs during the leave transition to update styles dynamically.
DIt runs before the leave transition starts to prepare the element.
Attempts:
2 left
💡 Hint
Think about when cleanup or final actions happen after an element disappears.
❓ state_output
expert
2:30remaining
What is the final opacity of the element after this enter transition completes?
Given this enter hook, what will be the element's opacity after the transition ends?
Vue
<template>
  <transition @enter="enter">
    <div v-if="show">Fade In</div>
  </transition>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(true)

function enter(el, done) {
  el.style.opacity = '0'
  el.style.transition = 'opacity 0.3s ease-in'
  requestAnimationFrame(() => {
    el.style.opacity = '1'
  })
  setTimeout(() => {
    done()
  }, 300)
}
</script>
A1
Bundefined (opacity style removed)
C0
D0.5
Attempts:
2 left
💡 Hint
Consider how CSS transitions change opacity from 0 to 1 over 0.3 seconds.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To run custom code at specific moments during enter and leave transitions -> Option A
  4. 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

  1. 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:.
  2. 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.
  3. Final Answer:

    <transition @before-enter="myHook"> -> Option C
  4. 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?
<transition @enter="onEnter">
  <div v-if="show">Hello</div>
</transition>

medium
A. Nothing is logged because show is initially false
B. Throws an error because done() is called synchronously
C. Logs 'Enter started' immediately on page load
D. Logs 'Enter started' after 1 second when element appears

Solution

  1. Step 1: Understand the timing of the show variable

    Initially, show is false, so the element is not rendered. After 1 second, show becomes true, triggering the enter transition.
  2. Step 2: Analyze the enter hook behavior

    The onEnter hook logs 'Enter started' and calls done() immediately. This happens after 1 second when the element appears.
  3. Final Answer:

    Logs 'Enter started' after 1 second when element appears -> Option D
  4. Quick Check:

    Enter hook logs on element show [OK]
Hint: Enter hook runs when element appears, not on load [OK]
Common Mistakes:
  • Thinking enter hook runs immediately on load
  • Assuming no log because initial show is false
  • Believing done() call causes error
4. Identify the error in this Vue transition JavaScript hook code:
<transition @leave="onLeave">
  <div v-if="visible">Bye</div>
</transition>

medium
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

  1. Step 1: Understand the leave hook timing

    The onLeave hook should call done() only after the animation finishes to keep the element during transition.
  2. 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.
  3. Final Answer:

    Calling done() immediately before animation finishes causes transition to end too soon -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Call apiCall(), then in .then() set opacity to 0 and call done() -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard