0
0
Vueframework~20 mins

Composable accepting parameters in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Vue composable usage?

Consider this composable that accepts a parameter and returns a reactive value:

import { ref } from 'vue'

export function useCounter(start) {
  const count = ref(start)
  function increment() {
    count.value++
  }
  return { count, increment }
}

In a component, we use it like this:

const { count, increment } = useCounter(5)
increment()
increment()
console.log(count.value)

What will be logged?

Vue
import { ref } from 'vue'

export function useCounter(start) {
  const count = ref(start)
  function increment() {
    count.value++
  }
  return { count, increment }
}

const { count, increment } = useCounter(5)
increment()
increment()
console.log(count.value)
A7
Bundefined
C2
D5
Attempts:
2 left
💡 Hint

Think about how many times increment() is called and what count starts at.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a composable accepting a parameter?

Which of these Vue composable definitions correctly accepts a parameter initialValue and returns a reactive value initialized with it?

A
export function useValue(initialValue) {
  const value = ref(initialValue)
  return { value }
}
B
export function useValue(initialValue) {
  const value = reactive(initialValue)
  return value
}
C
export function useValue() {
  const value = ref(initialValue)
  return { value }
}
D
export function useValue(initialValue) {
  const value = ref()
  value = initialValue
  return { value }
}
Attempts:
2 left
💡 Hint

Remember to initialize ref with the parameter and return it properly.

🔧 Debug
advanced
2:00remaining
Why does this composable not update the reactive value?

Look at this composable:

import { ref } from 'vue'

export function useToggle(initial) {
  let state = ref(initial)
  function toggle() {
    state = !state
  }
  return { state, toggle }
}

When used, calling toggle() does not update state.value. Why?

Vue
import { ref } from 'vue'

export function useToggle(initial) {
  let state = ref(initial)
  function toggle() {
    state = !state
  }
  return { state, toggle }
}
ABecause the function toggle is not called properly
BBecause ref cannot hold boolean values
CBecause the toggle function assigns a boolean to the ref variable instead of updating state.value
DBecause the initial parameter is not reactive
Attempts:
2 left
💡 Hint

Check how the toggle function changes state.

state_output
advanced
2:00remaining
What is the value of count after this composable usage?

Given this composable:

import { ref } from 'vue'

export function useStepCounter(start, step) {
  const count = ref(start)
  function advance() {
    count.value += step
  }
  return { count, advance }
}

And this usage:

const { count, advance } = useStepCounter(10, 3)
advance()
advance()
advance()
console.log(count.value)

What is logged?

Vue
import { ref } from 'vue'

export function useStepCounter(start, step) {
  const count = ref(start)
  function advance() {
    count.value += step
  }
  return { count, advance }
}

const { count, advance } = useStepCounter(10, 3)
advance()
advance()
advance()
console.log(count.value)
A13
B19
C10
Dundefined
Attempts:
2 left
💡 Hint

Count starts at 10 and increases by 3 each advance call.

🧠 Conceptual
expert
2:00remaining
Why pass parameters to a composable instead of hardcoding values?

In Vue, why is it better to pass parameters to a composable function rather than hardcoding values inside it?

AHardcoding values prevents accidental changes to reactive state
BHardcoding values improves performance by avoiding parameter parsing
CParameters are required to make the composable reactive
DPassing parameters makes the composable reusable with different data and easier to test
Attempts:
2 left
💡 Hint

Think about reusability and flexibility.