0
0
Vueframework~20 mins

Composable with reactive state in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Composable Master
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 and component setup. What will the component display after clicking the button twice?

Vue
import { ref } from 'vue';

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

// Component setup
import { useCounter } from './useCounter';
import { defineComponent } from 'vue';

export default defineComponent({
  setup() {
    const { count, increment } = useCounter();
    return { count, increment };
  },
  template: `<div><button @click="increment">Add</button><p>{{ count }}</p></div>`
});
A2
B1
C0
Dundefined
Attempts:
2 left
💡 Hint

Each click increases the count by 1 starting from 0.

📝 Syntax
intermediate
1:30remaining
Which option correctly defines a reactive state in a Vue composable?

Choose the correct way to create a reactive object with a name property initialized to 'Alice' inside a composable.

Aconst state = reactive('Alice');
Bconst state = reactive({ name: 'Alice' });
Cconst state = ref({ name: 'Alice' });
Dconst state = ref('name', 'Alice');
Attempts:
2 left
💡 Hint

Use reactive for objects and ref for primitives or single values.

🔧 Debug
advanced
2:30remaining
Why does this composable not update the component view?

Given this composable and component, why does the displayed count not update when calling increment?

Vue
import { reactive, toRefs } from 'vue';

export function useCounter() {
  const state = reactive({ count: 0 });
  function increment() {
    state.count += 1;
  }
  return { count: state.count, increment };
}

// Component setup
import { useCounter } from './useCounter';
import { defineComponent } from 'vue';

export default defineComponent({
  setup() {
    const { count, increment } = useCounter();
    return { count, increment };
  },
  template: `<div><button @click="increment">Add</button><p>{{ count }}</p></div>`
});
ABecause the component template syntax is incorrect
BBecause reactive objects cannot be used in composables
CBecause increment does not modify the count property
DBecause count is returned as a primitive value, not a reactive reference
Attempts:
2 left
💡 Hint

Check how count is returned and used in the component.

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

Using this composable, what is the value of count.value after calling increment() three times?

Vue
import { ref } from 'vue';

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

const { count, increment } = useCounter();
increment();
increment();
increment();
A9
B3
C6
D0
Attempts:
2 left
💡 Hint

Each increment adds 2 to count starting from 0.

🧠 Conceptual
expert
3:00remaining
Which statement best describes Vue composables with reactive state?

Choose the most accurate description of how composables manage reactive state in Vue.

AComposables encapsulate reactive state and logic, returning refs or reactive objects to maintain reactivity across components.
BComposables only return plain JavaScript objects and cannot hold reactive state.
CComposables must use Vuex to manage any reactive state shared between components.
DComposables automatically sync reactive state with the server without extra code.
Attempts:
2 left
💡 Hint

Think about how composables share reactive data and logic.