Consider a composable that returns a ref counter. If two components use this composable, what will happen to the counter state?
import { ref } from 'vue'; export function useCounter() { const count = ref(0); function increment() { count.value++; } return { count, increment }; }
Think about what happens when a function returns a new ref each time it is called.
Each call to the composable function creates a new ref instance. So each component using useCounter gets its own separate state.
Which option correctly shares a reactive count state across multiple components using a composable?
import { ref } from 'vue'; // Which composable shares the same count instance across components?
Where should the ref be declared to share state?
Declaring count outside the function creates a single shared instance. Declaring inside creates a new instance per call.
A composable exports a reactive object declared outside the function. The component uses it but the UI does not update when the state changes. What is the likely cause?
import { reactive } from 'vue'; const state = reactive({ count: 0 }); export function useState() { return state; } // In component: // const state = useState(); // state.count++; // But UI does not reflect changes.
Think about how Vue tracks reactivity when destructuring reactive objects.
Destructuring a reactive object breaks reactivity. Using toRefs preserves reactivity for each property.
Given this composable and two components using it, what is the final value of count.value after both components call increment() once?
import { ref } from 'vue'; const count = ref(0); export function useSingletonCounter() { function increment() { count.value++; } return { count, increment }; } // Component A calls increment() // Component B calls increment()
Remember the shared count is a single reactive reference.
Since count is declared outside the function, both components share it. Each increment adds 1, so total is 2.
You want to share a reactive state across components but also allow resetting it to initial values. Which pattern is best?
Think about how to keep one shared reactive state and still allow controlled changes.
Declaring reactive state outside the composable shares it. Exporting a reset function lets components reset state consistently.