Consider this composable and component setup. What will the component display after clicking the button twice?
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>` });
Each click increases the count by 1 starting from 0.
The composable useCounter returns a reactive count starting at 0. Each button click calls increment which adds 1. After two clicks, count is 2.
Choose the correct way to create a reactive object with a name property initialized to 'Alice' inside a composable.
Use reactive for objects and ref for primitives or single values.
reactive wraps an object to make all its properties reactive. Option B correctly creates a reactive object with a name property.
Given this composable and component, why does the displayed count not update when calling increment?
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>` });
Check how count is returned and used in the component.
The composable returns count: state.count which is a plain number, not reactive. The component sees a fixed value. To keep reactivity, return state or toRefs(state).
Using this composable, what is the value of count.value after calling increment() three times?
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();
Each increment adds 2 to count starting from 0.
Each call adds 2, so after 3 calls count is 0 + 2 + 2 + 2 = 6.
Choose the most accurate description of how composables manage reactive state in Vue.
Think about how composables share reactive data and logic.
Composables are functions that encapsulate reactive state and logic. They return refs or reactive objects so components using them stay reactive. Vuex is optional and composables do not sync automatically with servers.