Vue · Middle · Question 20 of 75
What is a composable?
export function useMouse() {
const x = ref(0), y = ref(0);
const update = (e) => { x.value = e.pageX; y.value = e.pageY; };
onMounted(() => window.addEventListener("mousemove", update));
onUnmounted(() => window.removeEventListener("mousemove", update));
return { x, y };
}- ✓Correct answer: A function using the Composition API to encapsulate and reuse stateful logic
- 2A component without a template
- 3A Vue 2 mixin rewritten in TypeScript
Explanation
Each call creates its own state. By convention composables are named useXxx, called synchronously in setup, and return refs. VueUse is a large collection of ready-made ones.