0
0
Vueframework~20 mins

Composable vs mixin comparison in Vue - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Composable vs Mixin Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Key difference between composables and mixins in Vue 3
Which statement best describes a key difference between composables and mixins in Vue 3?
AMixins are only usable in Vue 2; composables are exclusive to Vue 3 and cannot be used in Vue 2.
BComposables require class-based components; mixins work only with functional components.
CComposables automatically merge component templates; mixins only merge JavaScript logic.
DMixins merge data and methods into components causing potential naming conflicts; composables return reusable logic without merging, avoiding conflicts.
Attempts:
2 left
💡 Hint
Think about how data and methods are combined in components.
component_behavior
intermediate
2:00remaining
Output of component using mixin vs composable
Given the following Vue 3 component code, what will be the rendered output?
Vue
import { ref } from 'vue';

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

const counterMixin = {
  data() {
    return { count: 0 };
  },
  methods: {
    increment() {
      this.count++;
    }
  }
};

export default {
  name: 'TestComponent',
  mixins: [counterMixin],
  setup() {
    const { count: compCount, increment: compIncrement } = useCounter();
    return { compCount, compIncrement };
  },
  template: `<div>
    <p>Mixin count: {{ count }}</p>
    <button @click="compIncrement">Increment</button>
  </div>`
};
A<div><p>Mixin count: 0</p><button>Increment</button></div> and clicking button increments both mixin and composable counts
B<div><p>Mixin count: 0</p><button>Increment</button></div> and clicking button increments composable count but not displayed
C<div><p>Mixin count: 0</p><button>Increment</button></div> but clicking button causes error due to conflict
D<div><p>Mixin count: 0</p><button>Increment</button></div> and clicking button increments mixin count displayed
Attempts:
2 left
💡 Hint
Consider which count is displayed and which increment function is called.
lifecycle
advanced
2:00remaining
Lifecycle hook behavior in mixins vs composables
In Vue 3, how do lifecycle hooks behave differently when used inside mixins compared to composables?
AMixin lifecycle hooks are merged and called automatically; composable lifecycle hooks must be called inside setup() explicitly.
BComposable lifecycle hooks run before mixin hooks; mixin hooks run only once per app.
CMixin lifecycle hooks are ignored in Vue 3; only composable hooks run.
DComposable lifecycle hooks are merged like mixins but require manual registration.
Attempts:
2 left
💡 Hint
Think about how lifecycle hooks are declared and triggered in each pattern.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this mixin usage
What is the syntax error in the following Vue 3 component using a mixin?
const myMixin = {
  data() {
    return {
      message: 'Hello'
    }
  },
  methods: {
    greet() {
      console.log(this.message)
    }
  }
}

export default {
  name: 'MyComponent',
  mixins: myMixin,
  template: ``
}
Amethods must be declared inside setup() in Vue 3
Bdata() must be a function returning a function, not an object
Cmixins should be an array, so it should be mixins: [myMixin]
Dtemplate must be a separate .vue file, not inline string
Attempts:
2 left
💡 Hint
Check the type expected for the mixins option.
🔧 Debug
expert
3:00remaining
Debugging reactive state sharing issue between composables
Two components import the same composable which returns a reactive object. Both components modify the reactive object but changes do not reflect across components. Why?
Vue
export function useSharedState() {
  const state = reactive({ count: 0 });
  const increment = () => { state.count++ };
  return { state, increment };
}

// Component A and Component B both import and call useSharedState() separately
AEach component calls useSharedState() creating separate reactive objects; state is not shared.
BReactive objects cannot be shared between components in Vue 3.
CThe reactive object must be wrapped in ref() to share state.
DThe composable must be registered globally to share state.
Attempts:
2 left
💡 Hint
Consider how functions returning reactive objects behave when called multiple times.