What is the main benefit of using composables in Vue 3's Composition API?
Think about how you can share code between components easily.
Composables let you extract and reuse reactive logic in multiple components, avoiding repetition and improving maintainability.
Given this composable and component code, what will be the value of count after clicking the button twice?
import { ref } from 'vue'; export function useCounter() { const count = ref(0); function increment() { count.value++; } return { count, increment }; } // In component setup: const { count, increment } = useCounter(); // Button click calls increment()
Each click calls increment() which increases count by 1.
The count ref starts at 0 and increments by 1 on each button click, so after two clicks it becomes 2.
Which lifecycle hook is best to use inside a composable to run code when the component using it is mounted?
Consider when the DOM is ready for interaction.
onMounted runs after the component is mounted to the DOM, making it ideal for composables needing to run code at that time.
Which option shows the correct syntax for a Vue 3 composable that provides a reactive message and a function to update it?
Remember how to update a ref value properly.
Option A correctly uses ref and updates message.value. Other options misuse reactive or assign directly to ref variable.
Consider this composable used in multiple components. Why do all components share the same count value?
import { ref } from 'vue';
const count = ref(0);
export function useSharedCounter() {
function increment() {
count.value++;
}
return { count, increment };
}Think about where variables are declared and how that affects sharing.
Declaring count outside the composable function creates a single shared reactive variable used by all components, causing shared state.