0
0
Vueframework~20 mins

Composable with lifecycle hooks in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Composable Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the composable's onMounted hook runs?

Consider this Vue 3 composable using onMounted:

import { ref, onMounted } from 'vue'

export function useTimer() {
  const count = ref(0)
  onMounted(() => {
    count.value = 5
  })
  return { count }
}

If a component uses useTimer(), what will count.value be immediately after the component mounts?

A5
B0
Cundefined
DAn error occurs because <code>onMounted</code> is used outside setup
Attempts:
2 left
💡 Hint

Remember that onMounted runs after the component is mounted, so the value changes then.

state_output
intermediate
2:00remaining
What is the value of message after the composable's onUnmounted hook runs?

Given this composable:

import { ref, onUnmounted } from 'vue'

export function useMessage() {
  const message = ref('Hello')
  onUnmounted(() => {
    message.value = 'Goodbye'
  })
  return { message }
}

If a component uses useMessage() and then unmounts, what will message.value be?

Aundefined
B'Hello'
CAn error because you cannot change state in onUnmounted
D'Goodbye'
Attempts:
2 left
💡 Hint

Think about when onUnmounted runs and if state can be changed then.

📝 Syntax
advanced
2:00remaining
Which option correctly uses onBeforeUnmount inside a composable?

Choose the option that correctly registers a cleanup function with onBeforeUnmount in a Vue 3 composable.

A
import { onBeforeUnmount } from 'vue'

export function useCleanup() {
  onBeforeUnmount = () =&gt; {
    console.log('Cleaning up')
  }
}
B
import { onBeforeUnmount } from 'vue'

export function useCleanup() {
  onBeforeUnmount(() =&gt; {
    console.log('Cleaning up')
  })
}
C
import { onBeforeUnmount } from 'vue'

export function useCleanup() {
  onBeforeUnmount(() =&gt; {
    console.log('Cleaning up')
  })()
}
D
import { onBeforeUnmount } from 'vue'

export function useCleanup() {
  onBeforeUnmount(() =&gt; {
    console.log('Cleaning up')
  }
}
Attempts:
2 left
💡 Hint

Remember onBeforeUnmount is a function you call with a callback.

🔧 Debug
advanced
2:00remaining
Why does this composable's onMounted hook not run?

Look at this composable:

import { ref, onMounted } from 'vue'

export function useData() {
  const data = ref(null)
  onMounted(() => {
    data.value = 'Loaded'
  })
  return { data }
}

But when used in a component, data.value stays null. What is the likely cause?

AThe <code>onMounted</code> hook runs too early before the component mounts
BThe <code>ref</code> is not reactive, so changes don't update
CThe composable is called outside the component's setup function, so lifecycle hooks don't run
DThe composable must return a function to trigger <code>onMounted</code>
Attempts:
2 left
💡 Hint

Lifecycle hooks only work inside component setup or other lifecycle hooks.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using lifecycle hooks inside composables?

Why do Vue developers use lifecycle hooks like onMounted inside composables instead of only inside components?

ATo reuse lifecycle-dependent logic across multiple components easily
BTo force components to mount earlier than usual
CTo prevent components from unmounting automatically
DTo replace the component's own lifecycle hooks completely
Attempts:
2 left
💡 Hint

Think about how composables help organize code.